javascript - super() : expected 1-2 arguments, 上的 typescript 错误但得到 0

标签 javascript reactjs typescript

我不知道我需要做什么来解决这个 TS 错误:

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

declare const Ink: any;

export default class TableOfContents extends Component <{ id: string }, { rendered: boolean }> {
    constructor() {
        super();
        this.state = { rendered: false };
    }
enter image description here
更新 :
放在这里我感觉不太好,应该是什么?
enter image description here

最佳答案

The constructor for a React component is called before it is mounted. When implementing the constructor for a React.Component subclass, you should call super(props) before any other statement. Otherwise, this.props will be undefined in the constructor, which can lead to bugs.


https://reactjs.org/docs/react-component.html#constructor
所以,你需要写:
constructor(props) {
  super(props);
  this.state = { rendered: false };
}
你还可以为你的 props 和 state 定义接口(interface)来修复所有 TS 警告和更好的智能感知:
interface IProps {
  id: string
}

interface IState {
  rendered: boolean
}

export default class TableOfContents extends Component<IProps, IState> {
  constructor(props: IProps) {
    super(props)
    this.state = {
      rendered: false,
    }
  }
}
如果你的编辑提示并且不接受隐含的 any ,可以设置"noImplicitAny": falsecompilerOptions在 tsconfig 文件中。

关于javascript - super() : expected 1-2 arguments, 上的 typescript 错误但得到 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63312030/

相关文章:

javascript - React Router 和历史

javascript - React js从事件中调用函数

Typescript接口(interface)调用方法

javascript - 从indexedDB检索数据并将值返回给函数调用

javascript - 从 JSON 数组 react TreeView

javascript 选择选项功能在 IE 和 safari 中不起作用

javascript - 即使条件不满足,if 条件也会被执行

html - Angular 抛出 ExpressionChangedAfterItHasBeenCheckedError

import - Typescript requirejs web essentials 2.9

javascript - 度数 unicode 显示为问号