Tuple

元组的长度一定是固定的

基础

  1. let x:[string,number];
  2. x = ['hello',123];
  3. 可以使用对应的方法
  4. x[0].substr(0);

元组越界

  1. x[3] = 'world'; //出现错误,没有定义,undefined无法分配给string
  2. x.push(false); //出错
  3. x.push(222); //没报错,元组push的必须是之前定义的联合类型

可选元组类型

  1. let x:[string,number,boolean?];
  2. x = ['hi',1234];

声明

  1. interface没法用declareargs...
  2. declare function test(...args:[number,string,boolean]):void;
  3. 相当于
  4. declare function test(arg1:number,arg2:string,arg3:boolean):void;

不限制长度举例

  1. let str:[string,...number[]] = ['123',1,2,3];