Skip to content

useConsistentTypeDefinitions

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"style": {
"useConsistentTypeDefinitions": "error"
}
}
}
}

¥Description

强制类型定义始终使用 interfacetype

¥Enforce type definitions to consistently use either interface or type.

TypeScript 提供了两种定义对象类型的方式:interfacetype

¥TypeScript provides two different ways to define an object type: interface and type.

此规则强制对象类型定义始终使用 interfacetype。除了提高代码可读性之外,一致的类型定义风格还有助于减少开发者在不同代码库之间或大型代码库中切换时的认知负荷。

¥This rule enforces consistent usage of either interface or type for object type definitions. Consistent type definition styles, aside from improving code readability, help minimize cognitive load when developers switch between different codebases or within a large codebase.

¥Example

¥Invalid

type Point = { x: number; y: number; };
code-block.ts:1:1 lint/style/useConsistentTypeDefinitions  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Use of the type detected.

> 1 │ type Point = { x: number; y: number; };
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

The codebase should use a consistent coding style for the definition of types. This improves the readability and consistency.

Unsafe fix: Use interface.

1 - type·Point·=·{·x:·number;·y:·number;·};
1+ interface·Point·{
2+ ····x:·number;
3+ ····y:·number;
4+ }
2 5

¥Valid

interface Point {
x: number;
y: number;
}

¥Options

以下选项可用

¥The following options are available

此选项将确定类型定义使用的样式。

¥This option will determine which style to use for type definitions.

默认:interface

¥Default: interface

biome.json
{
"linter": {
"rules": {
"style": {
"useConsistentTypeDefinitions": {
"options": {
"style": "type"
}
}
}
}
}
}
interface Point {
x: number;
y: number;
}
code-block.ts:1:1 lint/style/useConsistentTypeDefinitions  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Use of the interface detected.

> 1 │ interface Point {
^^^^^^^^^^^^^^^^^
> 2 │ x: number;
> 3 │ y: number;
> 4 │ }
^
5 │

The codebase should use a consistent coding style for the definition of types. This improves the readability and consistency.

Unsafe fix: Use type alias.

1 - interface·Point·{
1+ type·Point·=·{
2 2 x: number;
3 3 y: number;
4 - }
4+ };
5 5

¥Related links