useConsistentTypeDefinitions
此内容尚不支持你的语言。
Summary
Section titled “Summary”- Rule available since:
v2.1.4
- Diagnostic Category:
lint/nursery/useConsistentTypeDefinitions
- This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
Description
Section titled “Description”Enforce type definitions to consistently use either interface
or type
.
TypeScript provides two different ways to define an object type: interface
and 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
Section titled “Example”Invalid
Section titled “Invalid”type Point = { x: number; y: number; };
code-block.ts:1:1 lint/nursery/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 │
interface Point { x: number; y: number;}
Options
Section titled “Options”The following options are available
This option will determine which style to use for type definitions.
Default: interface
{ "options": { "style": "type" }}
interface Point { x: number; y: number;}
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useConsistentTypeDefinitions": "error" } } }}