手册目录
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]; // 无错误
科技资讯
24小时阅读榜
1
2
3
4
5
6
7
8
9
10
精品课程
共5课时 | 18.1万人学习
共49课时 | 82.8万人学习
共29课时 | 65.3万人学习
共25课时 | 41.5万人学习
共43课时 | 77.2万人学习