Skip to content
← Back to rules

typescript/consistent-type-definitions Style

⚠️ 🛠️ A dangerous auto-fix is available for this rule for some violations.

它的作用

强制类型定义始终一致地使用 interfacetype 其中之一。

为什么这不好?

TypeScript 提供了两种常见的方式来定义对象类型:interfacetype。 这两者通常非常相似,并且经常可以互换使用。 始终使用相同的类型声明风格有助于提高代码可读性。

示例

默认情况下,此规则强制使用 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 };

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "typescript/consistent-type-definitions": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "typescript/consistent-type-definitions": "error",
  },
});
bash
oxlint --deny typescript/consistent-type-definitions

版本

此规则在 v0.2.17 中添加。

参考资料