是的,字符串数组的 InferType 无法按预期工作

问题描述

我正在尝试使用 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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...