简体   繁体   English

Typescript super() 上的错误:预期 1-2 arguments,但得到 0

[英]Typescript Error on super(): expected 1-2 arguments, but got 0

I don't know what I need to do to resolve this TS error:我不知道我需要做什么来解决这个 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 };
    }

在此处输入图像描述

UPDATE :更新

I don't feel good about putting any here, what should it be?放在这里我感觉不太好,应该是什么? 在此处输入图像描述

The constructor for a React component is called before it is mounted.在挂载之前调用 React 组件的构造函数。 When implementing the constructor for a React.Component subclass, you should call super(props) before any other statement.在为React.Component子类实现构造函数时,您应该在任何其他语句之前调用super(props) Otherwise, this.props will be undefined in the constructor, which can lead to bugs.否则, this.props将在构造函数中未定义,这可能会导致错误。

https://reactjs.org/docs/react-component.html#constructor https://reactjs.org/docs/react-component.html#constructor

So, you need to write:所以,你需要写:

constructor(props) {
  super(props);
  this.state = { rendered: false };
}

You can also define interfaces for your props and state to fix all TS warnings and better intellisense:您还可以为您的道具和 state 定义接口以修复所有 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,
    }
  }
}

If your editor complains and doesn't accept implicit any , you can set "noImplicitAny": false in compilerOptions in tsconfig file.如果您的编辑器抱怨并且不接受隐式any ,您可以在tsconfig文件的compilerOptions中设置"noImplicitAny": false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM