React using TypeScript, Don't use {} as a type. {

Issue

I am getting an ESlint error Don't use `{}` as a type. `{}` actually means "any non-nullish value". in my Contact component whiling using React and TypeScript.

The component doesn’t have any props.

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

I tried using null instead, but that threw other ESLint errors.

Solution

After a lot of digging I found the following GitHub issues.

By adding the following to your .eslintrc.js you are able to ignore the rule. It is recommended to use Configurations to have this rule only apply to React components. This will ensure strong typing everywhere else.

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

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

Answered By – Jordan Schnur

Answer Checked By – Marie Seifert (AngularFixing Admin)

Leave a Reply

Your email address will not be published.