TypeScript中interface与type有何本质区别?
- 内容介绍
- 文章标签
- 相关推荐
本文共计769个文字,预计阅读时间需要4分钟。
TypeScript 中,interface 和 type 是两种常用的类型定义方式。官方规范对它们的使用有一些区别:
- interface 可以在 extends 或 implements 子句中使用,而 object 类型的 type 别名则不能。- 例如,interface 可以这样使用:`interface Animal extends AnimalBase {}`,但 type 别名则不行。
总结如下:- interface 可用于 extends 和 implements。- type 别名不能用于 extends 和 implements。
interface VS type
大家使用 typescript 总会使用到 interface 和 type,官方规范 稍微说了下两者的区别
- An interface can be named in an extends or implements clause, but a type alias for an object type literal cannot.
- An interface can have multiple merged declarations, but a type alias for an object type literal cannot.
但是没有太具体的例子。
明人不说暗话,直接上区别。
本文共计769个文字,预计阅读时间需要4分钟。
TypeScript 中,interface 和 type 是两种常用的类型定义方式。官方规范对它们的使用有一些区别:
- interface 可以在 extends 或 implements 子句中使用,而 object 类型的 type 别名则不能。- 例如,interface 可以这样使用:`interface Animal extends AnimalBase {}`,但 type 别名则不行。
总结如下:- interface 可用于 extends 和 implements。- type 别名不能用于 extends 和 implements。
interface VS type
大家使用 typescript 总会使用到 interface 和 type,官方规范 稍微说了下两者的区别
- An interface can be named in an extends or implements clause, but a type alias for an object type literal cannot.
- An interface can have multiple merged declarations, but a type alias for an object type literal cannot.
但是没有太具体的例子。
明人不说暗话,直接上区别。

