TS2415:类'MyComponent'错误地扩展了基类
我创建了以下代码片段:
import * as React from "react";
export interface IMyProps {
}
export interface IMyState {
}
export class MyComponent extends React.Component<IMyProps, IMyState> {
constructor(props: IMyProps) {
super(props);
}
render() {
}
}
但TypeScript编译器给我以下错误:
(9,14):错误TS2415:类'MyComponent'错误地扩展了基类'Component' . 属性'render'的类型是不兼容的 . 类型'()=> void'不能赋值为'()=> false |元件' . 类型'void'不能赋值为'false |元件' .
这有什么不对?
1 回答
我不得不在render方法中添加一个return语句来编译它:
我认为这是因为因为在解释return语句之前不知道返回类型 . 在此之前,render()方法被解释为返回'void' .