TypeScript 数组

Source

1.数组的声明

let arr1:number[ ]     //声明一个数值类型的数组
let arr2:Array<string>  //声明一个字符串类型的数组

2.数组初始化

//定义一个空数组,数组容量为0
let arr1:number[] = [] 
//定义一个数组时,直接给数组赋值
let arr2:number[] = [1,2,3,4,5]
//定义数组 的同事给数组赋值
let arr3:Array<string> = ['jspang','wangyawei','ww']
let arr4:Array<boolean> = [ true,false,false]
let arr1:number[] = new Array()
let ara2:number[] = new Array(1,2,3,4,5)
let arr3:Array<string> = new Array('jspang','wangyawei','ww')
let arr4:Array<boolean> = new Array(true,false,false)

3.数组的项中不允许出现其他的类型

let  arr: number[] = [1, '1', 2, 3, 5];

// index.ts(1,5): error TS2322: Type '(number | string)[]' is not assignable to type 'number[]'.
//   Type 'number | string' is not assignable to type 'number'.
//     Type 'string' is not assignable to type 'number'.