手册
目录
readonly 关键字可以防止数组被更改。
const names: readonly string[] = ["Dylan"];
names.push("Jack"); // 错误:类型 'readonly string[]' 上不存在属性 'push'。
// 尝试删除 readonly 修饰符,看看是否有效?
如果数组有值,TypeScript 可以推断数组的类型。
const numbers = [1, 2, 3]; // inferred to type number[]
numbers.push(4); // 无错误
// 注释掉下面的行以查看成功的分配
numbers.push("2"); // 错误:类型为 'string' 的参数不能分配给类型为 'number' 的参数。
let head: number = numbers[0]; // 无错误
相关
视频
RELATED VIDEOS
科技资讯
1
2
3
4
5
6
7
8
9
精选课程
共5课时
17.2万人学习
共49课时
77万人学习
共29课时
61.7万人学习
共25课时
39.3万人学习
共43课时
70.9万人学习
共25课时
61.6万人学习
共22课时
23万人学习
共28课时
33.9万人学习
共89课时
125万人学习