Skip to content

Latest commit

 

History

History
31 lines (21 loc) · 611 Bytes

泛型数组.md

File metadata and controls

31 lines (21 loc) · 611 Bytes

泛型数组

Typescript对数组类型进行了扩展,可以用泛型语法声明数组:

Array<T>

// 只读数组
ReadonlyArray<T>

泛型数组语法比较简单,参考下面的例子:

// 定义数字数组
let arr: number[] = [1, 2, 3];

// 完全等价于
let arr: Array<number> = [1, 2, 3];

只读数组的值一旦初始化,就不能再改变

let arr: ReadonlyArray<number> = [1, 2, 3];

// 错误,只读数组的元素值不能改变
// error TS2542: Index signature in type 'ReadonlyArray<number>' only permits reading
arr[0] = 11;