-
Notifications
You must be signed in to change notification settings - Fork 935
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
How to enforce exact type schema in v1? #1913
Comments
This is more of a typescript limitation than a yup issue. Typescript doesn't have a way of specifying "exact types" if an interface is a subset of another then an object of one can be assigned to the other. interface Person {
name: string
age?: number
sex: 'male' | 'female' | 'other' | null
}
interface PersonWithExtraProperty {
name: string
age?: number
sex: 'male' | 'female' | 'other' | null
newProperty: number
}
const personWithExtraProperty: PersonWithExtraProperty = {
name: 'James',
age: 23,
sex: null,
newProperty: 4,
}
const person: Person = personWithExtraProperty // no error |
I think it would be more correct to add typing for a more strict schema definition, especially since developers had such a request before @types/yup: How to strongly-type ObjectSchema |
That is not what that code segment is doing. It's not overriding the type, if it was this would work: In any case i'm not sure what you want me to do or say. This is how typescript types work, i can't change that. |
I want to say that we need to find a way so that we can strict type the validation scheme. I spent some time on this but didn't find a solution.
I tried to define |
Typescript 4.9 has the |
Any updates on this topic? Why is there no answer to all such questions, is it not possible to protyp the schema using a pre-written interface? |
The answer is in the documentation. You can use a predefined interface just fine. The real question here is why does types riot lot have exact types, which is something I don't know the answer to nor can I control it. I can only work inside the typesystem |
|
According to the documentation, the scheme can be described as follows
but if a new property is added to the validation scheme, then the typescript does not throw an error
How to strongly-type schema in v1?
The text was updated successfully, but these errors were encountered: