admin管理员组文章数量:1355684
I'm converting a validation schema from jsx
to a tsx
filetype. It works perfectly in jsx
but in tsx
I can't get the type for the yup when
condition to pass. Even any
fails to pass. Any idea how to type this correctly?
The error appearing is
Argument of type '(startTime: Date) => Yup.DateSchema<Date | undefined, Record<string, any>, Date | undefined> | undefined' is not assignable to parameter of type 'ConditionOptions<RequiredDateSchema<Date | undefined, Record<string, any>>>'.
Type '(startTime: Date) => Yup.DateSchema<Date | undefined, Record<string, any>, Date | undefined> | undefined' is not assignable to type 'ConditionBuilder<RequiredDateSchema<Date | undefined, Record<string, any>>>'.
Type 'DateSchema<Date | undefined, Record<string, any>, Date | undefined> | undefined' is not assignable to type 'SchemaLike'. Type 'undefined' is not assignable to type 'SchemaLike'. TS2345
My validation:
Yup.date().required('This field is required')
.when('startTime', (startTime: Date) => { // <-- this is where error appears
if (startTime) {
return Yup.date()
.min(startTime, 'End must be after Start')
.typeError('End is required')
}
}),
I'm converting a validation schema from jsx
to a tsx
filetype. It works perfectly in jsx
but in tsx
I can't get the type for the yup when
condition to pass. Even any
fails to pass. Any idea how to type this correctly?
The error appearing is
Argument of type '(startTime: Date) => Yup.DateSchema<Date | undefined, Record<string, any>, Date | undefined> | undefined' is not assignable to parameter of type 'ConditionOptions<RequiredDateSchema<Date | undefined, Record<string, any>>>'.
Type '(startTime: Date) => Yup.DateSchema<Date | undefined, Record<string, any>, Date | undefined> | undefined' is not assignable to type 'ConditionBuilder<RequiredDateSchema<Date | undefined, Record<string, any>>>'.
Type 'DateSchema<Date | undefined, Record<string, any>, Date | undefined> | undefined' is not assignable to type 'SchemaLike'. Type 'undefined' is not assignable to type 'SchemaLike'. TS2345
My validation:
Yup.date().required('This field is required')
.when('startTime', (startTime: Date) => { // <-- this is where error appears
if (startTime) {
return Yup.date()
.min(startTime, 'End must be after Start')
.typeError('End is required')
}
}),
Share
Improve this question
edited Jan 19, 2022 at 0:57
user101289
asked Jan 18, 2022 at 21:23
user101289user101289
10.5k18 gold badges93 silver badges163 bronze badges
2 Answers
Reset to default 5Simplest thing:
Yup.date().required('This field is required')
.when('startTime', (startTime: Date) => {
if (startTime) {
return Yup.date()
.min(startTime, 'End must be after Start')
.typeError('End is required')
}
return Yup.date()
})
Personally I would prefer:
Yup.date()
.required("This field is required")
.when("startTime", (startTime) =>
startTime
? Yup.date()
.min(startTime, "End must be after Start")
.typeError("End is required")
: Yup.date()
);
but that is just clean up.
The reason Typescript show you this error is because you didn't specified the return type.
.when('x_lifetime_type', (values: string[], schema: NumberSchema): NumberSchema => {
if (values.indexOf('seconds') >= 0) {
return schema.min(60, 'Minimum value is 1 minute');
} else { return schema; }
})
In this example, input and output schema are typed with NumberSchema
. PD: You have to use schema
parameter to sum adicional validations.
本文标签: javascriptYup when validation with typescriptStack Overflow
版权声明:本文标题:javascript - Yup .when validation with typescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743964723a2569674.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论