【问题标题】:React using TypeScript, Don't use {} as a type. {使用 TypeScript 进行反应,不要使用 {} 作为类型。 {
【发布时间】:2021-06-20 17:48:03
【问题描述】:

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

组件没有任何道具。

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

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

【问题讨论】:

  • unknown 会这样做吗?或者甚至可能是never
  • 我从来没有尝试过,但未知的创建了它自己的问题。

标签: reactjs typescript eslint lint


【解决方案1】:

经过大量挖掘,我发现了以下 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

【讨论】:

    猜你喜欢
    • 2018-10-02
    • 2019-09-03
    • 2019-04-07
    • 2020-05-03
    • 2021-02-27
    • 2019-08-22
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多