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

Viewed 9560

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.

3 Answers

Expanding on Jordan answer to apply this rule to only React components in the .eslintrc.js file add

overrides: [
    {
        files: ['*.tsx, *.jsx'],
        rules: {
            '@typescript-eslint/ban-types': [
                'error',
                {
                    extendDefaults: true,
                    types: {
                        '{}': false,
                    },
                },
            ],
        },
    },
]

I don't think so, the chosen answer is correct. For anyone googling it, I'll leave it at what ESLint says:

  • If you want a type meaning "any object", you probably want Record<string, unknown> instead.
  • If you want a type meaning "any value", you probably want unknown instead.
  • If you want a type meaning "empty object", you probably want Record<string, never> instead.
Related