React使用TypeScript,不要使用{}作为类型。{

Don't use `{}` as a type. `{}` actually means "any non-nullish value".使用 React 和 TypeScript 时,我的 Contact 组件出现 ESlint 错误。

该组件没有任何道具。

export default class Contact extends React.Component<{}, MyState> {
    constructor(props = {}) {
        super(props);
        this.state = {
            signedUp: false,
        };
    }
}

我尝试使用null,但这引发了其他 ESLint 错误。

回答

经过大量挖掘,我发现了以下 GitHub 问题。

通过向您添加以下内容,您.eslintrc.js可以忽略该规则。建议使用Configurations使此规则仅适用于 React 组件。这将确保其他地方的强类型。

  "rules": {
    "@typescript-eslint/ban-types": [
      "error",
      {
        "extendDefaults": true,
        "types": {
          "{}": false
        }
      }
    ]
  }
}

来源:https : //github.com/typescript-eslint/typescript-eslint/issues/2063#issuecomment-675156492


以上是React使用TypeScript,不要使用{}作为类型。{的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>