ianstormtaylor / superstruct

A simple and composable way to validate data in JavaScript (and TypeScript).

Home Page:https://docs.superstructjs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Request: Infer the other way.

ivands opened this issue · comments

commented

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.

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.

commented

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.

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

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

commented

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?

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