Abdollah
Abdollah

Reputation: 5177

Why is "type" deprecated in typescript?

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 common 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?

Upvotes: 6

Views: 5335

Answers (2)

Haomin
Haomin

Reputation: 1559

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.org/docs/handbook/2/everyday-types.html#differences-between-type-aliases-and-interfaces

Upvotes: 1

Abdollah
Abdollah

Reputation: 5177

It seems that there is no problem with using type in Typescript and the rule is kind of opinionated as @CertainPerformance commented 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.

Upvotes: 4

Related Questions