在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
啊啊啊啊啊啊~,又吃撑,以后不吃饭了!
<----------------------------------------☺mizhiweixiao☺---------------------------------------->
1.数组和元组 1 let myName:string[] = ["wei","ze","yang"]; 2 let myAge:number[] = [21,22,23]; 3 let student:any[] = ["wei",23,true]; 元组是介样儿的: 1 let student:[string,number,boolean] = ["weizeyang",22,true]; ---- 星期五的尾巴,总是拉不长,哎! 2.赋值方式 (1)介样儿? 1 let student:[string,number,boolean]; 2 student[0] = "weizeyang"; 3 student[1] = 22; 4 5 // 也不影响对象类型的方法使用 6 student[0].slice(1); 7 student[1].toFixed(2); (2)介样儿? 1 let student1:[string,number,boolean] = ["weizeyang",22,true]; 2 3 // 但是这个时候就要注意了不可以少赋值 4 let student2:[string,number,boolean] = ["weizeyang",22]; 5 // Error : Type '[string, number]' is not assignable to type '[string, number, boolean]'. 6 // Erroe : Property '2' is missing in type '[string, number]'. (3)介样儿? 1 let student1:[string,number]; 2 student1 = ["weizeyang",22,"女"]; 3 4 // 这时需要注意 5 // 当赋值给越界的元素时,它类型会被限制为元组中每个类型的联合类型 6 7 let student2:[string,number]; 8 student2 = ["weizeyang",22,true]; 9 // Error : Type '[string, number, boolean]' is not assignable to type '[string, number]'. 还有一点需要注意的是: 1 let student1:[string,number]; 2 student1 = ["weizeyang",22,"女孩儿"]; 3 4 // 这时虽然赋值成功了,但是其方法使用时依然是 5 6 student1[2].slice(1); 7 // Error : Property 'slice' does not exist on type 'string | number'. 我记得学过这么一种解决办法: 1 let student1:[string,number]; 2 student1 = ["weizeyang",22,"女孩儿"]; 3 4 (<string>student1[2]).slice(1); // 孩儿
<----------------------------------------☺mizhiweixiao☺---------------------------------------->
|
请发表评论