问题描述
我正在尝试使用 Yup 来定义架构并生成可用于我的对象的 Typescript 类型。
使用 InferType 似乎对字符串和对象工作正常,但对于数组存在意外行为。当 required()
或 defined()
与 of()
一起使用时,这些类型将停止按预期工作。
注意:验证功能工作正常;我只是在使用 InferType 时遇到问题
理想:InferType 将所有操作组合到预期类型 string[]
const schema = yup.array().required().of(yup.string().required());
type SchemaType = yup.InferType<typeof schema>;
// Type should be `string[]`
const schemaInstance: SchemaType = ["string1"];
实际场景 1:数组为 string[] | undefined
const schema = yup.array().required().of(yup.string().required());
type SchemaType = yup.InferType<typeof schema>;
// Type is `string[] | undefined` so the following line compiles
const schemaInstance: SchemaType = undefined;
实际场景 2:数组为 any
const schema = yup.array().of(yup.string().required()).required();
type SchemaType = yup.InferType<typeof schema>;
// Type is `any` so the following line compiles
const schemaInstance: SchemaType = {};
有没有办法让这些类型对字符串数组按预期工作?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)