useConsistentTypeDefinitions
¥Summary
-
规则生效日期:
v2.1.4¥Rule available since:
v2.1.4 -
诊断类别:
lint/style/useConsistentTypeDefinitions¥Diagnostic Category:
lint/style/useConsistentTypeDefinitions -
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
¥How to configure
{ "linter": { "rules": { "style": { "useConsistentTypeDefinitions": "error" } } }}¥Description
强制类型定义始终使用 interface 或 type。
¥Enforce type definitions to consistently use either interface or type.
TypeScript 提供了两种定义对象类型的方式:interface 和 type。
¥TypeScript provides two different ways to define an object type: interface and type.
此规则强制对象类型定义始终使用 interface 或 type。除了提高代码可读性之外,一致的类型定义风格还有助于减少开发者在不同代码库之间或大型代码库中切换时的认知负荷。
¥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
{ "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
Biome v2.1 中文网 - 粤ICP备13048890号