【问题标题】:React Error: Type 'void' is not assignable to type 'Readonly<{}>' in React.ComponentReact 错误:类型“void”不可分配给 React.Component 中的类型“Readonly<{}>”
【发布时间】:2020-07-24 08:50:32
【问题描述】:

为什么会抛出下面的错误?

export default class DeleteModal extends React.Component<DeleteModalProps, void>

错误:

类型 'void' 不能分配给类型 'Readonly'

【问题讨论】:

  • 但是为什么会出错?

标签: reactjs typescript jsx


【解决方案1】:

解决方案

如果我们不需要组件中的状态,不要分配它就可以了。

React.Component<Props>

也可以设置空对象

interface State {}

React.Component<Props, State>

原因

  1. 如果你检查React.Component的来源index.d.ts
interface Component<P = {}, S = {}, SS = any> extends ComponentLifecycle<P, S, SS> { }

可以看到它被定义为空Object {}

  1. void的Typescript文档

声明 void 类型的变量没有用,因为您只能将 nullundefined 分配给它们

这就是该错误的原因。

【讨论】:

    猜你喜欢
    • 2020-07-22
    • 1970-01-01
    • 2016-06-24
    • 2021-02-06
    • 2020-07-29
    • 2018-01-04
    • 2019-06-07
    • 2019-10-06
    • 2020-06-02
    相关资源
    最近更新 更多