为什么TypeScript会在currentItem
构造函数,渲染函数等中看到状态属性ZoneEditor
?因此,TypeScript不会编译。
如果它很重要,我通过路由访问ZoneEditor组件。
interface ZoneEditorState {
currentItem: any;
}
class ZoneEditor extends React.Component<RouteComponentProps<{}>, ZoneEditorState> {
constructor() {
super();
this.state = { currentItem: null}; //here
}
render(){
return <XXX item={this.state.currentItem}/> //also here
}
}
错误:
Error TS2322 (TS) Type '{ currentItem: null; }' is not assignable to type '{ name: string; }'.
Object literal may only specify known properties, and 'currentItem' does not exist in type '{ name: string; }'.
和
Error TS2339 (TS) Property 'currentItem' does not exist on type '{ name: string; }'.
答案 0 :(得分:0)
不小心,不小心(不知道怎么做)我改变了我的react types
...查看州财产。
// Base component for plain JS classes
// tslint:disable-next-line:no-empty-interface
interface Component<P = {}, S = {}> extends ComponentLifecycle<P, S> { }
class Component<P, S> {
constructor(props?: P, context?: any);
// Disabling unified-signatures to have separate overloads. It's easier to understand this way.
// tslint:disable:unified-signatures
setState<K extends keyof S>(f: (prevState: S, props: P) => Pick<S, K>, callback?: () => any): void;
setState<K extends keyof S>(state: Pick<S, K>, callback?: () => any): void;
// tslint:enable:unified-signatures
forceUpdate(callBack?: () => any): void;
render(): JSX.Element | null | false;
// React.Props<T> is now deprecated, which means that the `children`
// property is not available on `P` by default, even though you can
// always pass children as variadic arguments to `createElement`.
// In the future, if we can define its call signature conditionally
// on the existence of `children` in `P`, then we should remove this.
props: Readonly<{ children?: ReactNode }> & Readonly<P>;
state: Readonly<S>; // this line was looking like state: { name: string };
context: any;
refs: {
[key: string]: ReactInstance
};
}