no-empty-interface
空のインターフェースの宣言を禁止します。
🎨
拡張 "plugin:@typescript-eslint/stylistic"
を ESLint 設定 で有効にすると、このルールが有効になります。
🔧
このルールによって報告された問題の一部は、 --fix
ESLint コマンドラインオプション.
によって自動的に修正可能です
提案
によって手動で修正できます。
TypeScript の空のインターフェースは、ほとんど何も行いません。null許容でないすべての値は
{}
に代入可能です。空のインターフェースを使用することは、{}
の概念を誤解したり、フィールドを埋め込むのを忘れたりするなど、プログラマーのエラーの兆候であることがよくあります。module.exports = {
"rules": {
"@typescript-eslint/no-empty-interface": "error"
}
};
このルールは、意味のあるインターフェースのみがコード内で宣言されるようにすることを目的としています。
.eslintrc.cjs
- このルールを Playground で試す ↗
- 例
// an empty interface
interface Foo {}
// an interface with only one supertype (Bar === Foo)
interface Bar extends Foo {}
// an interface with an empty list of supertypes
interface Baz {}
❌ 間違い// an interface with any number of members
interface Foo {
name: string;
}
// same as above
interface Bar {
age: number;
}
// an interface with more than one supertype
// in this case the interface can be used as a replacement of an intersection type.
interface Baz extends Foo, Bar {}
❌ 間違い✅ 正しい
Playground で開く
type Options = [
{
allowSingleExtends?: boolean;
},
];
const defaultOptions: Options = [{ allowSingleExtends: false }];
オプション
このルールは、以下のオプションを受け入れます
allowSingleExtends
allowSingleExtends: true
は、追加のメンバーを追加せずに単一のインターフェースを拡張することに関する警告を抑制します。