@typescript-eslint/no-empty-object-type

Disallow the {} type and empty interfaces

warn by defaulthas suggestions

Also called typescript-eslint/no-empty-object-type, typescript/no-empty-object-type

{} doesn't mean "an object with no properties". It accepts every value except null and undefined, so 0, "" and [] all pass. An interface with no members behaves the same way. Use object for "any object", unknown for "any value", and Record<string, never> for an object that has no properties.

An empty interface that extends one other type is reported too, because it's only a second name for that type.

{} in an intersection, such as T & {}, isn't reported. There it's a way to remove null and undefined from T.

This rule is on by default in TypeScript files.

Configure#

On by default as a warning. To turn it off:

bunfig.toml
[lint.rules]
"@typescript-eslint/no-empty-object-type" = "off" # or "error"

Every option, set to its default. The ones that are commented out are unset by default.

bunfig.toml
[lint.rules]
"@typescript-eslint/no-empty-object-type" = ["warn", {
  allowInterfaces = "never", # "always", "never" or "with-single-extends"
  allowObjectTypes = "never", # "always" or "never"
  # allowWithName = <string>,
}]

Examples#

example.ts2 problems
let settings: {} = 42; // no type error

interface Props {}
example.tsNo problems
let settings: object = { theme: "dark" };
let payload: unknown = 42;
let nothing: Record<string, never> = {};

type Defined<T> = T & {};

Options#

OptionDefaultWhat it does
allowInterfaces"never""always" allows every empty interface. "with-single-extends" allows one that extends one type.
allowObjectTypes"never""always" allows the {} type.
allowWithNamenoneA regular expression. An empty interface, or a type alias for {}, is allowed if its name matches.
example.tswith { "allowInterfaces": "with-single-extends" }No problems
interface User {
  name: string;
}

interface Admin extends User {}

With allowWithName, you can allow a component's empty props and nothing else:

example.tswith { "allowWithName": "Props$" }No problems
interface HeaderProps {}

type FooterProps = {};
example.tswith { "allowWithName": "Props$" }1 problem
interface HeaderState {}