Skip to main content

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.

eslint.config.mjs
export default tseslint.config({
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

选项

该规则接受以下选项:

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

资源