【发布时间】:2017-05-29 06:24:01
【问题描述】:
我将typescript 2.1 与JSX 一起使用,并尝试使用Partial 映射类型来扩展具有所需属性的接口的可选属性的反应组件接口。
这是具有所需属性的接口:
interface ActionsCreators {
A: string;
B: string;
};
这是我要扩展的接口:
interface Props extends React.Props<any> {
C: string;
};
这是映射类型:
type ActionsCreatorsPartial = Partial<ActionsCreators>;
给出这个结果:
type ActionsCreatorsPartial = {
A?: string;
B?: string;
}
这是我尝试做的:
interface Props extends React.Props<any>, ActionsCreatorsPartial {
C: string;
};
但我看到编译错误:“一个接口只能扩展一个类或另一个接口”如何将接口Props 与ActionsCreatorsPartial 类型连接?
【问题讨论】:
-
解决方法:
type Props = React.Props<any> & ActionsCreatorsPartial & { C: string; }; -
我认为你的评论应该是答案
标签: reactjs typescript types interface