元组
xcatliu opened this issue · 6 comments
tobemaster56 commented
因此使用元祖可以确定元素数据类型,但不要超出范围,可以把元祖理解为固定长度,超出范围不能保证其类型。
thatgirlismylove commented
那么请问,数组范型的联合类型或者没有any与元组有什么区别呢
bellakongqn commented
@thatgirlismylove
那么请问,数组范型的联合类型或者没有any与元组有什么区别呢
let tom: [string, number];
tom = ['Tom', 25]; // true
tony = [25, 'Tom'] // false
xcatliu commented
运行此代码会报错:
let tom: [string, number];
tom[0] = 'Tom';
tom[1] = 25;
tom[0].slice(1);
tom[1].toFixed(2);allenlinc commented
@xcatliu
运行此代码会报错:let tom!: [string, number]; tom[0] = 'Tom'; tom[1] = 25; tom[0].slice(1); tom[1].toFixed(2);
加个强制断言就好了,编译能通过
allenlinc commented
let tom!: [string, number];
tom = ['Jack',18];
tom[0] = 'Tom';
tom[1] = 25;
tom[0].slice(1);
tom[1].toFixed(2);
要赋值才能用 否则报错