• 元组
    • 简单的例子
    • 越界的元素
    • 参考

    元组

    数组合并了相同类型的对象,而元组(Tuple)合并了不同类型的对象。

    元组起源于函数编程语言(如 F#),在这些语言中频繁使用元组。

    简单的例子

    定义一对值分别为 stringnumber 的元组:

    1. let xcatliu: [string, number] = ['Xcat Liu', 25];

    当赋值或访问一个已知索引的元素时,会得到正确的类型:

    1. let xcatliu: [string, number];
    2. xcatliu[0] = 'Xcat Liu';
    3. xcatliu[1] = 25;
    4. xcatliu[0].slice(1);
    5. xcatliu[1].toFixed(2);

    也可以只赋值其中一项:

    1. let xcatliu: [string, number];
    2. xcatliu[0] = 'Xcat Liu';

    但是当直接对元组类型的变量进行初始化或者赋值的时候,需要提供所有元组类型中指定的项。

    1. let xcatliu: [string, number];
    2. xcatliu = ['Xcat Liu', 25];
    1. let xcatliu: [string, number] = ['Xcat Liu'];
    2. // index.ts(1,5): error TS2322: Type '[string]' is not assignable to type '[string, number]'.
    3. // Property '1' is missing in type '[string]'.
    1. let xcatliu: [string, number];
    2. xcatliu = ['Xcat Liu'];
    3. xcatliu[1] = 25;
    4. // index.ts(2,1): error TS2322: Type '[string]' is not assignable to type '[string, number]'.
    5. // Property '1' is missing in type '[string]'.

    越界的元素

    当赋值给越界的元素时,它类型会被限制为元组中每个类型的联合类型:

    1. let xcatliu: [string, number];
    2. xcatliu = ['Xcat Liu', 25, 'http://xcatliu.com/'];

    上面的例子中,数组的第三项满足联合类型 string | number

    1. let xcatliu: [string, number];
    2. xcatliu = ['Xcat Liu', 25];
    3. xcatliu.push('http://xcatliu.com/');
    4. xcatliu.push(true);
    5. // index.ts(4,14): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'.
    6. // Type 'boolean' is not assignable to type 'number'.

    当访问一个越界的元素,也会识别为元组中每个类型的联合类型:

    1. let xcatliu: [string, number];
    2. xcatliu = ['Xcat Liu', 25, 'http://xcatliu.com/'];
    3. console.log(xcatliu[2].slice(1));
    4. // index.ts(4,24): error TS2339: Property 'slice' does not exist on type 'string | number'.

    之前提到过,如果一个值是联合类型,我们只能访问此联合类型的所有类型里共有的属性或方法。

    参考

    • Basic Types # Tuple(中文版)