【发布时间】:2020-06-24 13:46:07
【问题描述】:
我已经定义了两种类型,比如 -
type A = $ReadOnly<{
a: ?$ReadOnlyArray<string>,
b: ?string,
}>;
和
type B = $ReadOnly<{
c: ?$ReadOnlyArray<boolean>,
d: ?number,
}>;
我想定义一个功能组件,它采用这两种类型中的任何一种,例如
type Input = {
eitherType: type can be A or B
};
function abcd({eitherType}: Input): React.Node {
// blah blah blah
// typecast to the particular type here
}
我不想使用any 来定义类型,有没有其他方法可以让这个变量同时具有这两种类型?
【问题讨论】:
-
这是打字稿吗?
-
eitherType: A | B。 typescriptlang.org/docs/handbook/advanced-types.html
标签: javascript reactjs