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

时间:2017-04-25 08:01:13

标签: typescript react-jsx jsx

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

class Cell extends Component<void, void> {
...
}

当我使用它时,即

<Cell />

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

相反,我试图断言返回值是Cell的实例,而不是JSX.Element,我该怎么做呢?

它失败了,因为它说

Type `Element` is not assignable to type `Cell`

1 个答案:

答案 0 :(得分:8)

  

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

使用React.ReactElement<Cell>。 e.g。

const foo: React.ReactElement<Cell> = <Cell/>; // Okay
const bar: React.ReactElement<Cell> = <NotCell/>; // Error!
相关问题