类型别名type

类型别名用来给一个类型起个新名字,使用type创建类型别名,类型别名不仅可以用来表示基本类型,还可以用来表示对象类型、联合类型、元祖和交集。让我们看一些例子:

type userName = string;
type userId = string | number;
// type arr = number[];
type arr = Array<number>;
//对象类型
type Person = {id: userId;name: userName;age: number;gender: string;isWebDev: boolean;
};type Tree<T> = { value: T };
const user: Person = {id: "901",name: "椿",age: 22,gender: "女",isWebDev: false,
}const numbers: arr = [1, 8, 9];

接口interface

接口是命名数据结构(例如对象)的另一种方式;与type不同,interface仅限于描述对象类型。

接口的声明语法也不同于类型别名的声明语法。让我们将上面的类型别名Person重写为接口声明:

interface Person {id: userId;name: userName;age: number;gender: string;isWebDev: boolean;
}

interface与type的相似之处

  • 都可以描述Object和Function
    两者都可以用来描述对象或函数,但语法不同:

type

type Point = {x: number;y: number;
}
type setPoint = (x: number, y: number) => void;

interface

interface Point {x: number;y: number;
}
interface SetPoint {(x: number, y: number): void
}
  • 二者都可以被继承
    interface和type都可以被继承。

另一个值得注意的是,接口和类型别名并不互斥。类型别名可以继承接口,反之亦然。只是在实现形式上,稍微有些差别。

interface继承interface

interface Person {name: string
}
interface Student extends Person {stuNo: number
}

interface继承type

type Person = {name: string;
};
interface Student extends Person {stuNo: number
}

type继承type

type Person = {name: string;
};
type Student = Person & {stuNo: number;
}

type继承interface

interface Person {name: string;
}
type Student = Person & {stuNo: number;
}
  • 实现implements
    类可以实现interface以及type(除联合类型外)
//interface
interface ICat {setName(name: string): void;
}class Cat implements ICat {setName(name: string): void {}
}//type
type ICat = {setName(name: string): void;
}class Cat implements ICat {setName(name: string): void {}
}

上面提到了特殊情况,类无法实现联合类型,是什么意思呢?

type Person = { name: string } | { setName(name: string): void };//无法对联合类型Person进行实现
//A class can only implement an object type or intersection of object types with statically known members.
class Student implements Person {name: '张三';setName(name: string): void {}
}

interface与type的区别

  • 定义基本类型别名
    type可以定义基本类型别名,但是interface无法定义,如:
type userName = string;
type stuNo = number;
  • 声明联合类型
    type可以声明联合类型,例如:
type Person = { name: string } | { setName(name: string): void };
  • 声明元祖
    type可以声明元祖类型
type data = [number, string];

以上都是type能做到,而interface做不到的,接下来聊聊type做不到的。

  • 声明合并
    如果你多次声明一个同名的借口,typescript会将它们合并到一个声明中,并将它们视为一个接口。这称为声明合并,例如:
interface Person {name: string;
}
interface Person {age: number;
}let user: Person = {name: 'zhangsan',age: 18,
}

这种情况下,如果是type的话,重复使用Person是会报错的:

type Person = { name: string };
type Person = { age: number };//Duplicate identifier 'Person'
  • 索引签名问题
    如果你经常使用typescript,一定遇到过相似的错误:

Type ‘xxx’ is not assignable to type ‘yyy’ Index signature is missing in type ‘xxx’.

例子:

interface propType {[key: string]: string;
}type dataType = {title: string;
};interface dataType1 {title: string;
}const data: dataType = {title: '订单页面',
};const data1: dataType1 = {title: '订单页面',
};let props: propType;
props = data;
props = data1;
//Error:类型“dataType1”不可分配给类型“propType”; 类型“dataType1”中缺少索引签名

我们发现dataType和dataType1对应的类型一样,但是interface定义的赋值失败,是什么原因呢?刚开始百思不解,最后在stack overflow上找到了一个相似的问题:

并且很幸运的找到了有效的答案:
Record<string,string>与{[key:string]:string}相同。只有当该类型的所有属性都已知并且可以对照该索引签名进行检查时,才允许将子集分配给该索引签名类型。在您的例子中,从exampleType到Record<string,sting的所有内容都是可以分配的。这只能针对对象字面量类型进行检查,因为一旦声明了对象字面量类型,就无法更改它们。因此,索引签名是已知的。

相反,在你使用interface去声明变量时,它们在那一刻类型并不是最终的类型。由于interfac可以进行声明合并,所以总有可能将新成员添加到同一个interface定义的类型上。

再结合

type 与 interface 的区别相关推荐

  1. type 与 interface 的区别,你真的懂了吗?

    大厂技术  高级前端  Node进阶 点击上方 程序员成长指北,关注公众号 回复1,加入高级Node交流群 在写 ts 相关代码的过程中,总能看到 interface 和 type 的身影.它们的作用 ...

  2. type 和 interface的区别

    类型别名(type)会给一个类型起个新名字. 类型别名有时和接口很像,但是可以作用于原始值,联合类型,元组以及其它任何你需要手写的类型. 1.都可以描述一个对象或者函数 [interface] int ...

  3. type和interface的区别

    type和interface都可以用来表示接口,但实际用的时候会有写差异. 1.type和interface的相同点:都是用来定义对象或函数的形状. interface example {name: ...

  4. TS 中 type 和 interface 的区别

    类型别名(type)会给一个类型起个新名字.类型别名有时和接口很像,但是可以作用于原始值,联合类型,元组以及其它任何你需要手写的类型. 1.都可以描述一个对象或者函数 [interface] inte ...

  5. typescript中type、interface的区别

    一.概念定义 interface:接口 在TS 中主要用于定义[对象类型],可以对[对象]的形状进行描述. type :类型别名 为类型创建一个新名称,它并不是一个类型,只是一个别名. 二,区别 in ...

  6. TypeScript type 和 interface区别

    在使用ts的type 和 interface时 两者作用(简单案例) interface只能定义对象数据结构类型. // 简单案例1 interface User {name: string;age: ...

  7. 【ts】typescript高阶:键值类型及type与interface区别

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 typescript高阶之键值类型及type与interface区别 前言 一.键值类型的语法 1.语法 2.错误例子 3.正确例子 ...

  8. type 和 interface区别

    interface只能定义对象数据结构类型. // 简单案例1 interface User {name: string;age: number;sex?: string; }let user: Us ...

  9. TypeScript中type和interface区别

    typescript中interface介绍:TypeScript 中的接口 interface_疆~的博客-CSDN博客通常使用接口(Interface)来定义对象的类型.https://blog. ...

  10. TypeScript 中 Type 和 Interface 有什么区别?

    type 和 interface type 是 类型别名,给一些类型的组合起别名,这样能够更方便地在各个地方使用. 假设我们的业务中,id 可以为字符串或数字,那么我们可以定义这么一个名为 ID 的 ...

最新文章

  1. 企业如何培养出得力的下属?
  2. Python Socket Programming
  3. linux mv命令改名,linux中mv命令使用详解(移动文件或者将文件改名)
  4. NLP-基础知识-005(专家系统)
  5. scala初学之函数定义、流程控制、异常处理入门
  6. 求你了,别再随便打日志了,教你动态修改日志级别!
  7. CUDA学习(六十五)
  8. 什么叫做石英表_什么是石英表 石英表是什么意思
  9. webview 转义字符_iOS中webView加载URL需要处理特殊字符
  10. 【ACL2020】BERT如何融合主题模型做文本匹配
  11. 中国游戏2022趋势报告:6大潜力领域4大发展趋势 多家企业分析
  12. 便携式手持 频谱分析仪_便携式频谱分析仪
  13. 无刷直流电机控制-->霍尔传感器
  14. c语言编程出彩色告白,C语言告白代码,一闪一闪亮晶晶~
  15. css所有属性大合集,包含中文标题
  16. LCD液晶屏显示接口总结
  17. 基于C语言设计的像素小鸟小游戏
  18. IE浏览器——莫名打不开
  19. 连花清瘟胶囊新冠应用 大健康医药·李喜贵:微量元素作用解密
  20. alsa 音频编程简单的例子 (总结)

热门文章

  1. 看到一些关于前端的书籍或者好的网站推荐
  2. 解决OneNote同步出错
  3. 视频录制——SurfaceView + MediaRecorder 实现视频录制功能
  4. oracle出现ora 12514,ora-12514报错解决方法
  5. java项目学生量化管理考核细则_班级管理量化考核细则范本
  6. python实现局域网内使用TCP服务器传输文件
  7. matlab单字音频合成,基于MATLAB的音频解析与合成
  8. 【北亚数据恢复】zfs文件系统的服务器误删除的数据恢复
  9. 邮箱显示exchange账号服务器错误,Exchange服务器刚开始用就总是出错!
  10. 利用Xmarks进行Chrome和Safari书签的同步