Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Infer the other way. #1159

Open
ivands opened this issue Dec 13, 2022 · 5 comments
Open

Feature Request: Infer the other way. #1159

ivands opened this issue Dec 13, 2022 · 5 comments
Labels
feature ♥ help please Issues that we'd love help solving

Comments

@ivands
Copy link

ivands commented Dec 13, 2022

The Infer function is great because it allows us to get the output types of a given struct.
However, it would also be great to be able to get the input types of a given struct.
I'll give an example of the difference:

The following struct will always output a number.

const MyNumber = coerce(number(), string(), (value) => parseFloat(value))
type OutputType = number // generated by Infer

However, it can accept numbers & strings.
So the input type would be:

type InputType = string | number

I need a way to Infer the input type of a struct.
Also, I think this would be a great addition to the already great lib.

@ianstormtaylor ianstormtaylor added feature ♥ help please Issues that we'd love help solving labels Jan 6, 2023
@ianstormtaylor
Copy link
Owner

Very cool idea, I'd be open to a pull request that implements this! Feel free to talk through the implementation here too to see if it will work or what the gotchas might be.

@ivands
Copy link
Author

ivands commented Jan 6, 2023

Sadly I'm just learning typescript, so I'm not sure if I can be much help atm. (This might change in the future XD)
However, I do know Zod already has a way to infer the input type of a given struct.
Maybe we can learn something from their implementation.
Superstruct still has a superior API tho, because of its tree-shakeablitity, making it ideal for serverless functions.

@ziad-saab
Copy link

@ianstormtaylor I took a stab at this here: #1181

It's probably not mergeable as-is, but lmk what you think.

@ivands
Copy link
Author

ivands commented Jun 10, 2023

Cool work @ziad-saab 👍.
I hope it can be merged soon because we really need this feature at the moment.

@ianstormtaylor What do you think?

@hmalik88
Copy link

Bump, would like this change to be added in. Have you had a change to look? @ianstormtaylor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ♥ help please Issues that we'd love help solving
Projects
None yet
Development

No branches or pull requests

4 participants