blog icon indicating copy to clipboard operation
blog copied to clipboard

TypeScript中的interface和type

Open kaindy7633 opened this issue 3 years ago • 0 comments

Table of Contents generated with DocToc

  • 相同点
    • 都可以描述一个对象或者函数
      • interface
      • type
    • 都允许拓展(extends
      • interface extends interface
      • type extends type
      • interface extends type
      • type extends interface
    • 不同点
      • type 可以而 interface 不行

typescript 总会使用到 interfacetype,官方规范稍微说了下两者的区别

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.

相同点

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

interface

interface User {
  name: string
  age: number
}

interface SetUser {
  (name: string, age: number): void;
}

type

type User = {
  name: string
  age: number
};

type SetUser = (name: string, age: number)=> void;

都允许拓展(extends

interfacetype 都可以拓展,并且两者并不是相互独立的,也就是说 interface 可以 extends type, type 也可以 extends interface 。 虽然效果差不多,但是两者语法不同。

interface extends interface

interface Name { 
  name: string; 
}
interface User extends Name { 
  age: number; 
}

type extends type

type Name = {
  name: string;
}
type User = Name & { age: number  };

interface extends type

type Name = { 
  name: string; 
}
interface User extends Name { 
  age: number; 
}

type extends interface

interface Name {
  name: string;
}
type User = Name & {
  age: number;
}

不同点

type 可以而 interface 不行

  • type 可以声明基本类型别名,联合类型,元组等类型

    / 基本类型别名
    ype Name = string
    
    / 联合类型
    nterface Dog {
    wong();
    
    nterface Cat {
    miao();
    
    
    ype Pet = Dog | Cat
    
    / 具体定义数组每个位置的类型
    ype PetList = [Dog, Pet]
    
  • type 语句中还可以使用 typeof 获取实例的 类型进行赋值

    / 当你想获取一个变量的类型时,使用 typeof
    et div = document.createElement('div');
    ype B = typeof div
    
  • 其他骚操作

    ype StringOrNumber = string | number;  
    ype Text = string | { text: string };  
    ype NameLookup = Dictionary<string, Person>;  
    ype Callback<T> = (data: T) => void;  
    ype Pair<T> = [T, T];  
    ype Coordinates = Pair<number>;  
    ype Tree<T> = T | { left: Tree<T>, right: Tree<T> };
    制代码interface 可以而 type 不行
    nterface 能够声明合并
    nterface User {
     name: string
     age: number
    
    
    nterface User {
     sex: string
    
    
    *
    ser 接口为 {
     name: string
     age: number
     sex: string 
    
    /
    

一般来说,如果不清楚什么时候用 interface/type,能用 interface 实现,就用 interface , 如果不能就用 type 。其他更多详情参看 官方规范文档

kaindy7633 avatar Mar 10 '21 15:03 kaindy7633