typescript/consistent-type-definitions 风格
作用
强制类型定义一致地使用 interface 或 type。
为什么这是问题?
TypeScript 提供了两种常见的定义对象类型的方式:interface 和 type。 这两种方式通常非常相似,经常可以互换使用。 一致地使用同一种类型声明风格有助于提高代码可读性。
示例
默认情况下,此规则强制对定义对象类型使用 interface。
错误 代码示例:
typescript
type T = { x: number };正确 代码示例:
typescript
type T = string;
type Foo = string | {};
interface T {
x: number;
}配置
此规则接受以下字符串值之一:
"interface"
优先使用 interface 而非 type 来定义对象类型:
typescript
interface T {
x: number;
}"type"
优先使用 type 而非 interface 来定义对象类型:
typescript
type T = { x: number };如何使用
通过配置文件或 CLI 启用 此规则,可以使用:
json
{
"rules": {
"typescript/consistent-type-definitions": "error"
}
}bash
oxlint --deny typescript/consistent-type-definitions