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

标签 reactjs typescript eslint lint

我收到一个 ESlint 错误 Don't use `{}` as a type. `{}` actually means "any non-nullish value".在我的 Contact 组件中使用 React 和 TypeScript。
该组件没有任何 Prop 。

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

关于reactjs - React 使用 TypeScript,不要使用 {} 作为类型。 {,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66773897/

相关文章:

javascript - react js Formik <Form/> resetForm() 不工作

reactjs - React-bootstrap - Form.Control defaultValue 重新渲染时未更新

angular - 使用 Angular 中的 RxJs,我只需要完成在组件中创建的 Observables 吗?

javascript - 通过动态属性从对象数组创建对象

Eslint 和 Prettier 冲突(eslint-config-prettier 不工作)

javascript - 从多个子组件的 componentDidMount() 同步更新父组件状态

javascript - 预期有一个赋值或函数调用,但却看到了一个表达式 - React

angular - 在 ngFor 中插入标签属性值总是会出现 "Got interpolation ({{}}) where expression was expected"错误

javascript - 使用 gulp-eslint 的问题

javascript - 有没有一种优雅的方式告诉 eslint 确保我们没有使用任何 ES6 语法/函数?