typescript

相同点

都可以描述一个对象或者函数

都允许拓展(extends

 

不同点

type 可以声明基本类型别名,

// 基本类型别名

 

type Name = string

 

// 联合类型

interface Dog {

    wong();

}

interface Cat {

    miao();

}

 

type Pet = Dog | Cat

 

// 具体定义数组每个位置的类型

type PetList = [Dog, Pet]

 

即type可以声明 基本类型,联合类型,元组 的别名,interface不行

 

interface 能够声明合并

interface User {

  name: string

  age: number

}

 

interface User {

  sex: string

}

 

/*

User 接口为 {

  name: string

  age: number

  sex: string 

}

*/

 

TypeScript提供了从旧类型中创建新类型的一种方式 — 映射类型

type Readonly<T> = {
    readonly [P in keyof T]: T[P];
}
type Partial<T> = {
    [P in keyof T]?: T[P];
}

type PersonPartial = Partial<Person>;
type ReadonlyPerson = Readonly<Person>;
posted @ 2021-01-20 00:04  二双  阅读(32)  评论(0编辑  收藏  举报