typescript/no-empty-interface 风格
它做了什么
禁止声明空接口。
为什么这是不好的?
在 TypeScript 中,空接口的作用非常有限:任何非可空值都可以赋值给 {}。使用空接口通常表明程序员出现了错误,例如误解了 {} 的概念,或忘记填充字段。此规则旨在确保代码中仅声明有意义的接口。
示例
此规则的错误代码示例:
ts
interface Foo {}
interface Bar extends Foo {}此规则的正确代码示例:
ts
interface Foo {
member: string;
}
interface Bar extends Foo {
member: string;
}配置
此规则接受一个配置对象,包含以下属性:
allowSingleExtends
type: boolean
默认值: false
当设置为 true 时,允许仅继承自一个接口的空接口。
如何使用
通过配置文件或 CLI 启用此规则,可以使用:
json
{
"rules": {
"typescript/no-empty-interface": "error"
}
}bash
oxlint --deny typescript/no-empty-interface