@typescript-eslint/consistent-type-definitions

Require either interface or type for object types, consistently

fixable

Also called typescript-eslint/consistent-type-definitions, typescript/consistent-type-definitions

You can declare an object's shape with interface User { ... } or with type User = { ... }. For most code the two work the same. This rule picks one. By default it's interface.

The rule only reports a type that's an object type. You can't write unions, functions, and other types as an interface.

--fix rewrites the declaration to the other form.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"@typescript-eslint/consistent-type-definitions" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
# "interface" or "type"
"@typescript-eslint/consistent-type-definitions" = ["error", "interface"]

Examples#

example.ts1 problem
type User = {
  name: string;
  email: string;
};
example.tsNo problems
interface User {
  name: string;
  email: string;
}

type Id = string | number;
type Handler = (user: User) => void;

Options#

OptionWhat it does
"interface" (default)Report type User = { ... }.
"type"Report every interface.
example.tswith "type"1 problem
interface User {
  name: string;
}
example.tswith "type"No problems
type User = {
  name: string;
};

When not to use it#

The two differ in a few ways. Two interface declarations with the same name merge, which is how you add to a global type such as Window. You can pass a type where a Record<string, unknown> is expected, but not an interface. If your code depends on both, leave this rule off.