如何断言React.Component实例的返回类型?

And*_* G. 2 jsx typescript react-jsx

在打字稿中,我有一个React Component,Cell,

class Cell extends Component<void, void> {
...
}
Run Code Online (Sandbox Code Playgroud)

当我使用它时,即

<Cell />
Run Code Online (Sandbox Code Playgroud)

我得到的返回类型是JSX.Element.

相反,我试图断言返回值是一个实例Cell,而不是JSX.Element,我怎么能这样做呢?

它失败了,因为它说

Type `Element` is not assignable to type `Cell`
Run Code Online (Sandbox Code Playgroud)

bas*_*rat 8

我试图断言返回值是Cell的一个实例,而不是JSX.Element

使用React.ReactElement<Cell>.例如

const foo: React.ReactElement<Cell> = <Cell/>; // Okay
const bar: React.ReactElement<Cell> = <NotCell/>; // Error!
Run Code Online (Sandbox Code Playgroud)