[英]In React with flowtype how do I type only certain props and allow any others
I'm making an input in form and want to type certain props for my input component but allow any others through without typing them - like all the attributes for an input field. 我正在以形式进行输入,并希望为我的输入组件键入某些道具,但允许其他任何类型都输入而无需键入它们-就像输入字段的所有属性一样。
Do I really need to create a type for all of them? 我真的需要为所有这些对象创建一个类型吗?
Flow supports subtype polymorphism. 流支持亚型多态性。 So if you define a type like this
因此,如果您定义这样的类型
type Foo = {
a: number,
b: string,
}
Any object that has at least those fields, will be a valid Foo
: 至少具有这些字段的任何对象都将是有效的
Foo
:
let foo: Foo = {
a: 0,
b: "hello",
c: false // this is ok
}
However, if you actually need to access those fields then you should make them part of the type. 但是,如果您实际上需要访问这些字段,则应将它们作为类型的一部分。
You can define an intersection type of custom props + any input props: 您可以定义自定义道具+任何输入道具的交集类型:
type Props = {
foo: string,
bar: number,
} & $Shape<HTMLInputElement>
Copies the shape of the type supplied, but marks every field optional.
复制提供的类型的形状,但将每个字段都标记为可选。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.