eslint-plugin-interface-to-type
v1.0.2
Published
An ESLint plugin to enforce the use of type over interface in TypeScript.
Downloads
179
Maintainers
Readme
eslint-plugin-interface-to-type
An ESLint plugin that enforces the use of type
aliases instead of interface
declarations in TypeScript.
Installation
Install the plugin as a dev dependency:
npm install eslint-plugin-interface-to-type --save-dev
Usage
- Add the plugin to your ESLint configuration file:
{
"plugins": ["interface-to-type"]
}
- Enable the rule in your ESLint configuration:
{
"rules": {
"interface-to-type/prefer-type-over-interface": "error"
}
}
Rule: prefer-type-over-interface
This rule enforces the use of type
aliases over interface
declarations in TypeScript. See the full documentation in the docs/rules/prefer-type-over-interface.md
.
Incorrect Code
interface User {
id: number;
name: string;
role: 'admin' | 'user';
}
Correct Code
type User = {
id: number;
name: string;
role: 'admin' | 'user';
};
Why interface
can be risky
One of the reasons to prefer type
over interface
is the potential for unexpected behavior with interface
merging. In TypeScript, when two interface
declarations share the same name, they are automatically merged into a single declaration. This behavior, known as Declaration Merging, can introduce subtle bugs and unintended behaviors in your codebase.
Example of Declaration Merging
interface User {
id: number;
}
interface User {
name: string;
}
// Resulting type
interface User {
id: number;
name: string;
}
While this behavior can sometimes be useful, it often leads to confusion and makes code harder to maintain, especially in large or collaborative projects. Using type
avoids this risk entirely, as type
declarations with the same name will result in a compilation error.
Autofix
This rule supports ESLint's --fix
option, which can automatically convert interface
declarations to equivalent type
aliases.
License
This project is licensed under the MIT License.