eslint-plugin-interface-method-style
v1.1.5
Published
ESLint plugin to enforce consistent method style between interfaces and their implementations
Downloads
1,593
Maintainers
Readme
eslint-plugin-interface-method-style
ESLint rule to enforce consistent method implementation styles between TypeScript interfaces and their implementations.
Table of Contents
Introduction
In large TypeScript codebases, maintaining consistency between interfaces and their implementations is crucial for code readability and maintainability. This ESLint plugin ensures that methods and properties defined in TypeScript interfaces or type aliases are implemented using the same syntax in classes and object literals.
Features
- 🎯 Method Style Consistency: Ensures consistent method and property styles.
- 🚀 Supports Classes and Object Literals: Works with classes and object literals.
- ✨ TypeScript Compatibility: Supports TypeScript interfaces and type aliases.
Installation
npm
npm install eslint-plugin-interface-method-style --save-dev
Yarn
yarn add eslint-plugin-interface-method-style --dev
pnpm
pnpm add eslint-plugin-interface-method-style --save-dev
Usage
Add interface-method-style
to your ESLint configuration.
Config (eslint.config.mjs)
import interfaceMethodStyle from "eslint-plugin-interface-method-style";
export default [
// ...
interfaceMethodStyle.configs.recommended,
];
Legacy Config (.eslintrc.json)
Add interface-method-style
to the plugins section of your configuration file. You can omit the eslint-plugin-
prefix:
{
"plugins": ["interface-method-style"]
}
Then configure the rules you want to use under the rules section.
{
"rules": {
"interface-method-style/interface-method-style": "error"
}
}
Rule Details
This rule enforces that the implementation of methods and properties matches their definitions in the interfaces or type aliases:
- Methods defined with
method(): void
must be implemented as methods - Properties defined with
method: () => void
must be implemented as arrow functions
✅ Correct
interface Foo {
method(): void;
property: () => void;
}
class Bar implements Foo {
method() {
console.log("method");
}
property = () => {
console.log("property");
};
}
❌ Incorrect
interface Foo {
method(): void;
property: () => void;
}
class Bar implements Foo {
method = () => {
console.log("method");
};
property() {
console.log("property");
}
}