我试图validationSchema通过使用为 Yup 生成可重用的类型定义ObjectSchema,但我不断收到错误
使用 Yup 文档中的示例 https://github.com/jquense/yup#ensuring-a-schema-matches-an-existing-type
interface TestValidationSchema {
title: string;
}
const validationSchema: Yup.ObjectSchema<TestValidationSchema> = Yup.object({
title: Yup.string().required()
});
...
return (
<Formik
initialValues={initialValues}
onSubmit={handleSubmit}
validationSchema={validationSchema}
>
...
Run Code Online (Sandbox Code Playgroud)
错误
Type 'ObjectSchema<Shape<object | undefined, { title: string; }>, object>' is not assignable to type 'ObjectSchema<TestValidationSchema, object>'.
Types of property 'fields' are incompatible.
Type '{ title: Schema<string, object>; } | undefined' is not assignable to type '{ title: Schema<string, object>; }'.
Type 'undefined' is not assignable to type '{ title: Schema<string, object>; }'.
Run Code Online (Sandbox Code Playgroud)
我尝试将 的版本升级Yup到 32.11 并得到一个不同的但仍然令人困惑的错误,这让我认为我只是使用ObjectSchema不正确
Type 'OptionalObjectSchema<{ title: RequiredStringSchema<string | undefined, AnyObject>; }, AnyObject, TypeOfShape<{ title: RequiredStringSchema<string | undefined, AnyObject>; }>>' is not assignable to type 'ObjectSchema<TestValidationSchema, AnyObject, TypeOfShape<TestValidationSchema>, AssertsShape<TestValidationSchema>>'.
The types of 'fields.title' are incompatible between these types.
Type 'RequiredStringSchema<string | undefined, AnyObject>' is not assignable to type 'string'.ts(2322)
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏。
更新
我刚刚检查了上面返回的类型validationSchema,下面的类型有效。我仍然想知道为什么 Yup 文档中的示例对我不起作用
type ValidationSchema = Yup.ObjectSchema<{
title: string;
} | undefined, object>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9026 次 |
| 最近记录: |