-
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
Incorrect types using SchemaOf with DateSchema #1183
Comments
ah looks like Date is being inferred to an ObjectSchema incorrectly here |
Similar error with mixed types
|
So ultimately this may be a limit of the approach. TS doesn't distinguish have a way (AFICT) to distinguish between any ol js object and class instances. We can change the order to inference to whitelist the common types like Date, but the mixed case for something like File, may be impossible :/ |
@jquense May be definition of
With this definition |
the issue there is |
Is this the same error, or should I open a new issue? code export interface Participant {
email: string;
firstName: string;
locale: Locale;
gender?: Gender;
}
const participantSchema: yup.ObjectSchema<Participant> = yup
.object()
.shape({
firstName: yup.string().min(2).required().default(''),
email: yup.string().email().default('').required(),
locale: yup
.string()
.oneOf(Object.values(Locale))
.default(Locale.german)
.required(),
gender: yup.string().oneOf(Object.values(Gender)).required()
})
.required(); Error:
This error showed up when trying to bump from 0.30.0 to 0.32.8 along with several other similar errors. |
@PupoSDC that is different, you are using the generic incorrectly on ObjectSchema |
I'm not sure if this could help, but it seems this only happens with const schema: SchemaOf<{ file: File | null }> = object({
file: Yup.mixed<File | null>().defined()
}); The same happens with const schema1: SchemaOf<{ date: Date | null }> = object({
date: date().defined()
});
const schema2: SchemaOf<{ date?: Date }> = object({
date: date().notRequired()
}); But more curious is that the following code does NOT complain in TypeScript 🤔 const schema3: SchemaOf<{ date?: Date }> = object({
date: date().required()
});
Anyways, I hope this really helps somehow to solve this issue. Cheers! |
Any update on this? The interface Base {
id: number;
due: Date;
amount: number;
reversed: boolean;
}
const BaseSchema: SchemaOf<Base> = object({
id: number(),
due: date(),
amount: number(),
reversed: boolean(),
}); |
This also blocks creating a custom schema for I get why this is complicated, but limits the intended functionality. |
Added a more generic fix in #1358 |
Is there a way to force proper types in yup? @cipacda 's link seems to hint at it but doesn't provide any instructions. The workaround I'm currently using involves // @ts-ignore - override correct yup type
export const requiredDateSchema: yup.SchemaOf<Date> = yup.date().required();
interface MyDate {
date: Date;
}
const schema: yup.SchemaOf<MyDate> = yup.object().shape({
date: requiredDateSchema,
}); |
@plaa do you also have a solution to make this work with optional dates? The solution below, does not work because of the current typings.
|
@philipp985 I tried fiddling with I really wish yup would have some syntax to force typing when by default it goes wrong. I've had to type in an inordinate amount of |
Ok, that's what I also tried. Except I am now sticking to |
Seems to be fixed with 0.32.11 |
Working for me on |
An example from above gives typing errors in
Error:
While this code has no type errors:
|
Describe the bug
Incorrect types using SchemaOf with DateSchema
To Reproduce
See example https://codesandbox.io/s/kaldy-dgj5g?file=/src/index.ts
Expected behavior
Types will match
Platform (please complete the following information):
The text was updated successfully, but these errors were encountered: