接口和高级类型

接口

接口定义

TypeScript 的核心之一就是对值(数据)所具有的结构进行类型检查,除了基本标注,针对对象类型的数据,还可以通过 Interface (接口),来进行标注。

接口:对复杂的对象类型进行标注的一种方式,或者给其它代码定义一种契约(比如:类)
接口的基础语法定义结构特别简单

  1. interface Point {
  2. x: number;
  3. y: number; }

上面的代码定义了一个类型,该类型包含两个属性,一个 number 类型的 x 和一个 number 类型的 y,接口中多个属性之间可以使用 逗号 或者 分号 进行分隔
我们可以通过这个接口来给一个数据进行类型标注

  1. let p1: Point = {
  2. x: 100,
  3. y: 100 };

注意:接口是一种 类型 ,不能作为 值 使用

  1. interface Point {
  2. x: number;
  3. y: number; }
  4. let p1 = Point; //错误
  5. //当然,接口的定义规则远远不止这些

可选属性

接口也可以定义可选的属性,通过 ? 来进行标注

  1. interface Point {
  2. x: number;
  3. y: number;
  4. color?: string;
  5. //其中的 color? 表示该属性是可选的
  6. }

只读属性

我们还可以通过 readonly 来标注属性为只读

  1. interface Point {
  2. readonly x: number;
  3. readonly y: number;
  4. }
  5. //当我们标注了一个属性为只读,那么该属性除了初始化以外,是不能被再次赋值的

任意属性

有的时候,我们希望给一个接口添加任意属性,可以通过索引类型来实现
数字类型索引

  1. interface Point {
  2. x: number;
  3. y: number;
  4. [prop: number]: number;
  5. }

字符串类型索引

  1. interface Point {
  2. x: number;
  3. y: number;
  4. [prop: string]: number;
  5. }

数字索引是字符串索引的子类型

  1. //注意:索引签名参数类型必须为 string 或 number 之一,但两者可同时出现
  2. interface Point {
  3. [prop1: string]: string;
  4. [prop2: number]: string;
  5. }
  1. //注意:当同时存在数字类型索引和字符串类型索引的时候,数字类型的值类型必须是字符串类型的值类型或子类型
  2. interface Point1 {
  3. [prop1: string]: string;
  4. [prop2: number]: number; //错误
  5. }
  6. interface Point2 {
  7. [prop1: string]: Object;
  8. [prop2: number]: Date; // 正确 }

使用接口描述函数

我们还可以使用接口来描述一个函数

  1. //注意,如果使用接口来单独描述一个函数,是没 key 的
  2. interface IFunc {
  3. (a: string): string;
  4. }
  5. let fn: IFunc = function(a) {}

接口合并

多个同名的接口合并成一个接口

  1. //如果合并的接口存在同名的非函数成员,则必须保证他们类型一致,否则编译报错
  2. //接口中的同名函数则是采用重载
  3. interface Box {
  4. height: number;
  5. width: number;
  6. }
  7. interface Box {
  8. scale: number;
  9. }
  10. let box: Box = {height: 5, width: 6, scale: 10}

高级类型

联合类型

联合类型也可以称为多选类型,当我们希望标注一个变量为多个类型之一时可以选择联合类型标注,或的关系

  1. function css(ele: Element, attr: string, value: string|number) {
  2. // ...
  3. }
  4. let box = document.querySelector('.box');
  5. // document.querySelector 方法返回值就是一个联合类型 if (box) {
  6. // ts 会提示有 null 的可能性,加上判断更严谨 css(box, 'width', '100px');
  7. css(box, 'opacity', 1);
  8. css(box, 'opacity', [1,2]); // 错误
  9. }

交叉类型

交叉类型也可以称为合并类型,可以把多种类型合并到一起成为一种新的类型,并且 的关系 对一个对象进行扩展:

  1. interface o1 {x: number, y: string};
  2. interface o2 {z: number};
  3. let o: o1 & o2 = Object.assign({}, {x:1,y:'2'}, {z: 100});
  4. //上面这个assign方法在ES3中是没有的所以我们需要引入相应的lib 或者是转化target

Tips

TypeScript 在编译过程中只会转换语法(比如扩展运算符,箭头函数等语法进行转换,对于
API 是不会进行转换的(也没必要转换,而是引入一些扩展库进行处理的),如果我们的代码中 使用了 target 中没有的 API ,则需要手动进行引入,默认情况下 TypeScript 会根据
target 载入核心的类型库
target 为 es5 时: [“dom”, “es5”, “scripthost”]
target 为 es6 时: [“dom”, “es6”, “dom.iterable”, “scripthost”]
如果代码中使用了这些默认载入库以外的代码,则可以通过 lib 选项来进行设置

http://www.typescriptlang.org/docs/handbook/compiler-options.html

字面量类型配合联合类型

  1. function setPosition(ele: Element, direction: 'left' | 'top' | 'right' |
  2. 'bottom') {
  3. // ...
  4. }
  5. // ok
  6. box && setDirection(box, 'bottom');
  7. // error
  8. box && setDirection(box, 'hehe');

类型别名(type)

有的时候类型标注比较复杂,这个时候我们可以类型标注起一个相对简单的名字

  1. type dir = 'left' | 'top' | 'right' | 'bottom';
  2. function setPosition(ele: Element, direction: dir) {
  3. // ...
  4. }

使用类型别名(type)定义函数类型

  1. //这里需要注意一下,如果使用 type 来定义函数类型,和接口有点不太相同
  2. type callback = (a: string) => string;
  3. let fn: callback = function(a) {};
  4. // 或者直接
  5. let fn: (a: string) => string = function(a) {}

interface 与 type 的区别

interface

  • 只能描述object/class/function 的类型
  • 同名 interface 自动合并,利于扩展

type

  • 不能重名
  • 能描述所有数据

类型推导

每次都显式标注类型会比较麻烦,TypeScript 提供了一种更加方便的特性:类型推导。TypeScript 编译 器会根据当前上下文自动的推导出对应的类型标注,这个过程发生在:

  • 初始化变量
  • 设置函数默认参数值
  • 返回函数值
  1. // 自动推断 x 为 number
  2. let x = 1;
  3. // 不能将类型“"a"”分配给类型“number” x = 'a';
  4. // 函数参数类型、函数返回值会根据对应的默认值和返回值进行自动推断 function fn(a = 1) {return a * a}

类型断言

有的时候,我们可能标注一个更加精确的类型(缩小类型标注范围),比如:

  1. let img = document.querySelector('#img');

我们可以看到 img 的类型为 Element,而 Element 类型其实只是元素类型的通用类型,如果我们去访 问 src 这个属性是有问题的,我们需要把它的类型标注得更为精确:HTMLImageElement 类型,这个 时候,我们就可以使用类型断言,它类似于一种 类型转换:

  1. let img = <HTMLImageElement>document.querySelector('#img');
  2. //或者
  3. let img = document.querySelector('#img') as HTMLImageElement;
  4. //注意:断言只是一种预判,并不会数据本身产生实际的作用,即:类似转换,但并非真的转换了