I'm trying to handle model validation in typescript. I'd like to be able to capture the nested types of the validation definition.
For example, I want to be able to create the validator like this.
const validateUser = createValidator({
name: {
first: {
value: "First"
},
last: {
value: "Last"
}
},
age: {
value: 32
},
hasOnboarded: {
value: false
}
});
This would create a validateUser function that takes a model of the specified type and validates its types.
I'd like to be able to capture the type so that validateUser knows to accept, objects that conform to the interface.
type ValidateUser = typeof validateUser;
Should be type
(
model: {
name: {
first: string,
last: string
},
age: number,
hasOnboarded: boolean
}
) => boolean
Is this possible in TypeScript?