数组类型
一般类型
类型[]
1
| let arr1:string[] = ['1', 'hello', 'world']
|
数组泛型
Array<类型>
1 2 3 4 5 6
| interface Person { name:string, age:number }
let personArr:Array<Person> = [{name:'hhh', age:111}, {name:'aaa', age:22}]
|
用接口表示数组
一般用来描述类数组,因为下标可以为任意数字
1 2 3 4
| interface NumberArray { [index: number]: number; } let fibonacci: NumberArray = [1, 1, 2, 3, 5];
|
多维数组
1
| let arr_2:any[][] = [[1,'2',false],['q',3]]
|
arguments类数组
1 2 3 4 5 6 7 8 9 10 11 12 13
| function Arr(...args:any):void { console.log(args); console.log(args[0]) console.log(args[1]) console.log(args[2]) let arr:IArguments = arguments; console.log(arr); console.log(arr[0]); console.log(arr[1]); console.log(arr[2]) }
Arr(1,'22',333);
|
输出
1 2 3 4 5 6 7 8
| [ 1, '22', 333 ] 1 22 333 [Arguments] { '0': 1, '1': '22', '2': 333 } 1 22 333
|
arguments是ts自带属性,将参数包装成IArguments对象