我收到一个 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/