consistent-type-definitions
Enforce type definitions to consistently use either
interface
ortype
.
There are two ways to define a type.
// type alias
type T1 = {
a: string;
b: number;
};
// interface keyword
interface T2 {
a: string;
b: number;
}
Attributes
- Included in configs
- ✅ Recommended
- 🔒 Strict
- Fixable
- 🔧 Automated Fixer
- 💡 Suggestion Fixer
- 💭 Requires type information
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/consistent-type-definitions": "warn"
}
};
Options
This rule accepts an options string of the following possible values:
type Options = "interface" | "type";
const defaultOptions: Options = ["interface"];
"interface"
(default): enforce usinginterface
s for object type definitions."type"
: enforce usingtype
s for object type definitions.
interface
- ❌ Incorrect
- ✅ Correct
/* eslint @typescript-eslint/consistent-type-definitions: ["error", "interface"] */
type T = { x: number };
/* eslint @typescript-eslint/consistent-type-definitions: ["error", "interface"] */
type T = string;
type Foo = string | {};
interface T {
x: number;
}
type
- ❌ Incorrect
- ✅ Correct
/* eslint @typescript-eslint/consistent-type-definitions: ["error", "type"] */
interface T {
x: number;
}
/* eslint @typescript-eslint/consistent-type-definitions: ["error", "type"] */
type T = { x: number };
When Not To Use It
If you specifically want to use an interface or type literal for stylistic reasons, you can disable this rule.