Logo
Section 1 / 7

数组的类型标注(orientation)

本节课讲解如何定义数组类型。

导入

数组的类型标注(orientation)

本节课讲解如何定义数组类型。

多种类型的数组类型标注

数组的类型标注与普通变量类似,但当数组包含多种类型时,需要使用括号。

1const arr1: string[] = ["test", "one"]; 2 3const arr2: (string | number)[] = [1, 2, "three"];

嵌套数组类型

数组可以包含任意类型,包括嵌套的数组。

1const arr3: (Date | string[])[] = [new Date(), new Date(), ["1", "a"]];

泛型数组

数组也可以是泛型的,通过 T[] 或 Array<T> 来定义类型。

1function action<T>(options: T[]): void {} 2 3// 与以下写法相同: 4 5function action2<T>(options: Array<T>): void {}

迭代器

带有跳过值的数组