TypeScript 数组类型定义:两种常用方法
TS中的数组类型定义可以使用以下两种方式:
- 使用数组泛型
let arr: Array<number> = [1, 2, 3]; // 数组元素类型为number
- 使用元素类型和方括号
let arr: number[] = [1, 2, 3]; // 数组元素类型为number
其中,Array<number> 和 number[] 是等价的。
除了上述方式,还可以使用如下方式定义元素类型为任意类型的数组:
let arr: any[] = [1, 'hello', true];
原文地址: https://www.cveoy.top/t/topic/lLFf 著作权归作者所有。请勿转载和采集!