typescript 结构子类型

编程语言的子类型,分为两种:

名义子类型 Java和C#中就是名义子类型,必须显示继承,用来extends才是子类型

结构子类型 只要结构相同,就是子类型

typescript是结构子类型。

type Foo = {
    age: number
}

type Bar = {
    age: number
}

是一样的类型,不需要extends。

type Foo = {
    name: string,
    age: number
}

type Bar = {
    name: string
}

Foo是Bar的子类型。

原文地址:https://www.cnblogs.com/mengff/p/12936830.html