【发布时间】:2016-09-23 08:39:50
【问题描述】:
我为一些 reactJS 组件创建了我的打字稿定义,我看到在 react.d.ts 文件中有 2 个接口:
interface ComponentClass<P> {
new(props?: P, context?: any): Component<P, ComponentState>;
propTypes?: ValidationMap<P>;
contextTypes?: ValidationMap<any>;
childContextTypes?: ValidationMap<any>;
defaultProps?: P;
displayName?: string;
}
和:
interface ClassicComponentClass<P> extends ComponentClass<P> {
new(props?: P, context?: any): ClassicComponent<P, ComponentState>;
getDefaultProps?(): P;
}
我看到 ClassicComponentClass 扩展了 ComponentClass,但是什么时候应该使用其中之一呢? (为组件创建定义时)这是否取决于组件的创建方式?
【问题讨论】:
-
为什么你需要任何一个?为什么不使用
Component?这两个是组件类的接口而不是实例。在大多数情况下,您可以通过扩展Component创建自己的组件 -
但我不创建自己的组件。我只为它创建定义。例如:声明模块“react-whatever”{ interface reactWhaterverProps{ id:number;名称:字符串; }\n 让 reactWhaterver: __React.ComponentClass
\n 导出默认的 reactWhaterver;所以在这种情况下我不需要创建一些“状态”接口,因为它不是我的组件,它的状态对我来说是黑盒子,对吗? -
为什么不
React.Component<ReachWhateverProps>?你到底想做什么? -
打字稿错误是不可能的,因为“组件”有 2 个参数
。我只需要
。我在DefiniteTyped 项目中为react 创建了一些定义。这是我的一个例子:github.com/DefinitelyTyped/DefinitelyTyped/blob/master/…
标签: javascript reactjs interface typescript definition