no-empty-interface
禁止宣告空介面。
🎨
延伸 「plugin:@typescript-eslint/風格"
於 ESLint 組態 啟用此規則。
🔧
有些問題是由此規則所回報的,而問題可經由 --fix
ESLint 命令行選項自動修復.
💡
有些問題是由此規則所回報的,而問題可經由編輯器 建議手動修復.
TypeScript 中的空介面無太多效用:所有非可為 `null` 的值都可指定給 {}
。使用空介面通常表示程式設計師錯誤,例如誤解 {}
的概念或忘記填寫欄位。
此規則旨在確保在程式碼中僅宣告有意義的介面。
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-empty-interface": "error"
}
};
在試驗場上嘗試這項規則 ↗
範例
- ❌ 不正確
- ✅ 正確
// 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 {}
在試驗場上開啟選項
這項規則接受下列選項
type Options = [
{
allowSingleExtends?: boolean;
},
];
const defaultOptions: Options = [{ allowSingleExtends: false }];
allowSingleExtends
allowSingleExtends: true
會取消有關擴充單一介面且未新增成員的警告
不可使用的時機
如果您不介意擁有空/無意義的介面,則不需要這條規則。