consistent-type-definitions
Enforces type definitions to consistently use either interface
or type
.
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
Options
This rule accepts one string option:
"interface"
: enforce usinginterface
s for object type definitions."type"
: enforce usingtype
s for object type definitions.
For example:
{
// Use type for object definitions
"@typescript-eslint/consistent-type-definitions": ["error", "type"]
}
interface
Examples of code with interface
option.
- ❌ Incorrect
- ✅ Correct
type T = { x: number };
type T = string;
type Foo = string | {};
interface T {
x: number;
}
type
Examples of code with type
option.
- ❌ Incorrect
- ✅ Correct
interface T {
x: number;
}
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.
Related To
- TSLint: interface-over-type-literal