admin管理员组

文章数量:1403371

I have a type declaration as below:

type Position = {
    white: number[];
    black: number[];
}

When I lint the project, I see this error:

error  Use an interface instead of a type literal  @typescript-eslint/prefer-interface

The documentation about the rule that causes the error says:

Interfaces are generally preferred over type literals because interfaces can be implemented, extended and merged.

This rule is mon between TSLint and ESLint. I know that interface is more powerful than type, but when I don't need interface advantages and type is enough, why shouldn't I use it? Are there any other drawbacks to using type?

I have a type declaration as below:

type Position = {
    white: number[];
    black: number[];
}

When I lint the project, I see this error:

error  Use an interface instead of a type literal  @typescript-eslint/prefer-interface

The documentation about the rule that causes the error says:

Interfaces are generally preferred over type literals because interfaces can be implemented, extended and merged.

This rule is mon between TSLint and ESLint. I know that interface is more powerful than type, but when I don't need interface advantages and type is enough, why shouldn't I use it? Are there any other drawbacks to using type?

Share Improve this question asked Sep 15, 2019 at 6:04 AbdollahAbdollah 5,2073 gold badges33 silver badges52 bronze badges 3
  • 3 This is quite an opinionated subject, see the thread here: github./typescript-eslint/typescript-eslint/issues/433 One reason to use types is that they can be mapped, unlike interfaces, I think – CertainPerformance Commented Sep 15, 2019 at 6:06
  • 2 in typescript, there is really no difference between an interface and type. They can both extend each other. The error can be turned off and is more of a remendation than for any performance reasons. – smac89 Commented Sep 15, 2019 at 6:08
  • Also have a look here and the linked in further question for a parison type vs interface. – ford04 Commented Sep 15, 2019 at 9:17
Add a ment  | 

2 Answers 2

Reset to default 4

It seems that there is no problem with using type in Typescript and the rule is kind of opinionated as @CertainPerformance mented above. I figured out that the rule has been deprecated and removed from version 2.2.0.

I use @typescript-eslint/eslint-plugin and @typescript-eslint/parser. I upgraded both of them to version 2.2.0 and got rid of the linter error.

With the difference between Type and Interface only on extendibility (not exactly the only difference, but a major difference that was mentioned by the documentation). I don't see a reason for keeping the Type anymore, maybe that is why it's deprecated.

Differences Between Type Aliases and Interfaces

Type aliases and interfaces are very similar, and in many cases, you can choose between them freely. Almost all features of an interface are available in type, the key distinction is that a type cannot be re-opened to add new properties vs an interface which is always extendable.

Reference: https://www.typescriptlang/docs/handbook/2/everyday-types.html#differences-between-type-aliases-and-interfaces

本文标签: javascriptWhy is quottypequot deprecated in typescriptStack Overflow