【发布时间】:2018-10-12 02:31:58
【问题描述】:
我正在尝试为 React 组件的 props 使用联合类型
type Props =
| {
type: "string"
value: string
}
| {
type: "number"
value: number
}
| {
type: "none"
}
class DynamicProps extends React.Component<Props> {
render() {
return null
}
}
// Ok
const string_jsx = <DynamicProps type="string" value="hello" />
// Error as expected, value should be a string
const string_jsx_bad = <DynamicProps type="string" value={5} />
// Ok
const number_jsx = <DynamicProps type="number" value={5} />
// Error as expcted value should be a number
const number_jsx_bad = <DynamicProps type="number" value="hello" />
// Error as expected, invalid isn't a property on any of the unioned types
const extra = <DynamicProps type="string" value="extra" invalid="what" />
// No error? There should be no value when type="none"
const none_jsx = <DynamicProps type="none" value="This should be an error?" />
// Ok, seems like value has become optional
const none2_jsx = <DynamicProps type="none" />
// Error as expected, value is not present. Value doesn't seem to be made optional all the time
const required = <DynamicProps type="string" />
它似乎部分起作用,因为取决于 type 道具,有效道具会改变。然而,虽然没有出现在联合中的任何类型上的额外属性将是一个错误,但似乎出现在至少一个联合类型中但不属于基于判别属性的类型将不是错误。
我不确定为什么会这样。使用联合类型作为 react 组件的 props 是一种反模式吗?
【问题讨论】:
标签: reactjs typescript