【发布时间】:2020-01-16 02:05:13
【问题描述】:
我知道以下代码块是等效的。
export interface Person {
id: number;
name: string;
}
export type Person = {
id: number;
name: string;
}
我知道Typescript: Interfaces vs Types,但不知道何时使用type 而不是interface。猜猜type 的存在是有原因的。
https://www.typescriptlang.org/docs/home.html 上的手册没有提到type。我也无法在 https://duckduckgo.com/?q=site%3Atypescriptlang.org+type 上找到文档。是否有可能 type 已被弃用以支持 interface?
谢谢!
【问题讨论】:
-
@AndrewMarshall 我知道这个问题,但它没有回答何时在接口上使用类型。
-
列出了两者的区别。如果您需要某些功能,请选择提供它们的功能。如链接答案中所述,在某些情况下,一个人只能使用一种而不能使用另一种。例如,不能复制
type Foo = number和interface,也不能复制interface与type合并。 -
TypeScript 入门这么多方面有点抽象,但感谢分享@sagar.acharya。
标签: typescript