在TypeScript中,可以像这样组合两种接口类型
interface Foo {
var1: string
}
interface Bar {
var2: string
}
type Combined = Foo & Bar
我不想组合键,而是想将键从一个接口排除到另一个接口.你能用打字脚本做吗?
原因是,我有一个HOC,它管理像这样的其他包装组件的属性值
export default function valueHOC<P> (
Comp: React.ComponentClass<P> | React.StatelessComponent<P>
): React.ComponentClass<P> {
return class WrappedComponent extends React.Component<P, State> {
render () {
return (
<Comp
{...this.props}
value={this.state.value}
/>
)
}
}
有了它我就可以写作了
const ValuedComponent = valueHOC(MyComponent)
然后
<ValuedComponent />
但问题是,返回的组件类型也使用了给定组件的props类型,所以TypeScript会抱怨并要求我提供value
prop.因此,我将不得不写一些
<ValuedComponent value="foo" />
无论如何都不会使用该值.我想要的是返回一个没有特定键的界面,我想要这样的东西
React.ComponentClass<P - {value: string}>
那么返回的组件中就不需要value
.现在可以用打字脚本吗?