let tom: [string,number];tom[0] ='Tom';tom[1] =25;tom[0].slice(1);tom[1].toFixed(2);
也可以只賦值其中一項:
let tom: [string,number];tom[0] ='Tom';
但是當直接對元組型別的變數進行初始化或者賦值的時候,需要提供所有元組型別中指定的項。
let tom: [string,number];tom = ['Tom',25];
let tom: [string,number];tom = ['Tom'];// Property '1' is missing in type '[string]' but required in type '[string, number]'.
越界的元素
當新增越界的元素時,它的型別會被限制為元組中每個型別的聯合型別:
let tom: [string,number];tom = ['Tom',25];tom.push('male');tom.push(true);// Argument of type 'true' is not assignable to parameter of type 'string | number'.