no-empty-interface
禁止声明空接口.
此规则报告的一些问题可通过 --fix
ESLint 命令行选项自动修复。
此规则报告的一些问题可以通过编辑器 建议 手动修复。
此规则已被弃用,以支持更全面的 @typescript-eslint/no-empty-object-type
规则。
¥This rule has been deprecated in favour of the more comprehensive @typescript-eslint/no-empty-object-type
rule.
TypeScript 中的空接口几乎没有什么作用:任何非空值都可以分配给 {}
。使用空接口通常是程序员错误的标志,例如误解 {}
的概念或忘记填写字段。
¥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.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/no-empty-interface": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-empty-interface": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ 错误
- ✅ 正确
// 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选项
该规则接受以下选项:
type Options = [
{
/** Whether to allow empty interfaces that extend a single other interface. */
allowSingleExtends?: boolean;
},
];
const defaultOptions: Options = [{ allowSingleExtends: false }];
¥Options
allowSingleExtends
Whether to allow empty interfaces that extend a single other interface. Default: false
.
allowSingleExtends: true
将静音有关扩展单个接口而不添加其他成员的警告。
¥allowSingleExtends: true
will silence warnings about extending a single interface without adding additional members.
何时不使用它
¥When Not To Use It
如果你不关心空/无意义的接口,那么你将不需要此 规则。
¥If you don't care about having empty/meaningless interfaces, then you will not need this rule.
相关
¥Related To