no-empty-interface
Disallow the declaration of empty interfaces.
在 ESLint 配置 中扩展"plugin:@typescript-eslint/stylistic"
可启用此规则。
此规则报告的一些问题可通过 --fix
ESLint 命令行选项自动修复。
此规则报告的一些问题可以通过编辑器 建议 手动修复。
TypeScript 中的空接口几乎没有什么作用: 任何不可为 null 的值都可以分配给 {}
。
使用空接口通常是程序员错误的标志,例如误解了 {}
的概念或忘记填写字段。
英:An empty interface in TypeScript does very little: any non-nullable value is assignable to {}
.
Using an empty interface is often a sign of programmer error, such as misunderstanding the concept of {}
or forgetting to fill in fields.
该规则旨在确保代码中仅声明有意义的接口。
英:This rule aims to ensure that only meaningful interfaces are declared in the code.
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 {}
Open in Playground// 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 {}
Open in Playground选项
allowSingleExtends
allowSingleExtends: true
将消除有关扩展单个接口而不添加其他成员的警告
英:allowSingleExtends: true
will silence warnings about extending a single interface without adding additional members
何时不使用它
如果你不关心空/无意义的接口,那么你将不需要此规则。
英:If you don't care about having empty/meaningless interfaces, then you will not need this rule.
选项
该规则接受以下选项
type Options = [
{
allowSingleExtends?: boolean;
},
];
const defaultOptions: Options = [{ allowSingleExtends: false }];