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
?
- 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
2 Answers
Reset to default 4It 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
版权声明:本文标题:javascript - Why is "type" deprecated in typescript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744353228a2602173.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论