@typescript-eslint/consistent-type-definitions
Require either interface or type for object types, consistently
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:
[lint.rules]
"@typescript-eslint/consistent-type-definitions" = "error" # or "warn"Every option, set to its default:
[lint.rules]
# "interface" or "type"
"@typescript-eslint/consistent-type-definitions" = ["error", "interface"]Examples#
type UserUse an interface instead of a type. = {
name: string;
email: string;
};interface User {
name: string;
email: string;
}
type Id = string | number;
type Handler = (user: User) => void;Options#
| Option | What it does |
|---|---|
"interface" (default) | Report type User = { ... }. |
"type" | Report every interface. |
"type"1 probleminterface UserUse a type instead of an interface. {
name: string;
}"type"No problemstype 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.