类型定义似乎在 react-hook-form 7 中不起作用
Type definition does not seem to work in react-hook-form 7
我定义了表单字段的类型。我有 3 个不同的字段,有 3 个不同的类型。在启动表单后,每个字段似乎对所有 3 种不同类型都很有价值。怎么了?
type Inputs = {
val1: string
val2: number
val3: boolean
};
鼠标悬停类型显示:
const val1: string | number | boolean
@vuvu 您的代码运行良好。你得到的行为正在发生,因为 watch 正在返回一个数组。如果您放置了不同类型的项目,那么您可以在数组中放置所有类型的项目。您可以阅读此 tutorial 作为补充阅读 material。
如其所说,
Of course, you can always initialize an array like shown below, but you will not get the advantage of TypeScript's type system.
let arr = [1, 3, 'Apple', 'Orange', 'Banana', true, false];
@vuvu 此行为已在 react-hook-form
的 7.0.6
版本中修复,现在可以正确检索类型。 (参考this PR)
我定义了表单字段的类型。我有 3 个不同的字段,有 3 个不同的类型。在启动表单后,每个字段似乎对所有 3 种不同类型都很有价值。怎么了?
type Inputs = {
val1: string
val2: number
val3: boolean
};
鼠标悬停类型显示:
const val1: string | number | boolean
@vuvu 您的代码运行良好。你得到的行为正在发生,因为 watch 正在返回一个数组。如果您放置了不同类型的项目,那么您可以在数组中放置所有类型的项目。您可以阅读此 tutorial 作为补充阅读 material。
如其所说,
Of course, you can always initialize an array like shown below, but you will not get the advantage of TypeScript's type system.
let arr = [1, 3, 'Apple', 'Orange', 'Banana', true, false];
@vuvu 此行为已在 react-hook-form
的 7.0.6
版本中修复,现在可以正确检索类型。 (参考this PR)