4

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?

Abdollah
  • 4,579
  • 3
  • 29
  • 49
  • 3
    This is quite an opinionated subject, see the thread here: https://github.com/typescript-eslint/typescript-eslint/issues/433 One reason to use types is that they can be mapped, unlike interfaces, I think – CertainPerformance Sep 15 '19 at 06: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 recommendation than for any performance reasons. – smac89 Sep 15 '19 at 06:08
  • Also have a look [here](https://stackoverflow.com/questions/57936452/has-type-been-deprecated-in-favor-of-interface) and the linked in further question for a comparison type vs interface. – ford04 Sep 15 '19 at 09:17

2 Answers2

2

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.

Abdollah
  • 4,579
  • 3
  • 29
  • 49
0

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

Haomin
  • 1,265
  • 13
  • 12