Skip to content
静心静心
HOME
DoubtfulCases
github icon
  • Typescript基础

    • 原始数据类型
      • 任意值
        • 类型推论
          • 联合类型
            • 对象的类型——接口
              • 数组的类型
                • 「类型 + 方括号」表示法
                  • 数组泛型
                    • 用接口表示数组
                      • 类数组
                        • any 在数组中的应用
                          • 参考
                          • 函数的类型
                            • 类型断言
                              • 声明文件
                                • 内置对象
                                • Typescript进阶

                                  • 类型别名
                                    • 字符串字面量类型
                                      • 元组
                                        • 枚举
                                          • 类
                                            • 类与接口
                                              • 泛型
                                                • 声明合并
                                                  • 扩展阅读

                                                  数组的类型

                                                  author iconYYtimer icon大约 2 分钟

                                                  此页内容
                                                  • 「类型 + 方括号」表示法
                                                  • 数组泛型
                                                  • 用接口表示数组
                                                  • 类数组
                                                  • any 在数组中的应用
                                                  • 参考

                                                  # 数组的类型

                                                  在 TypeScript 中,数组类型有多种定义方式,比较灵活。

                                                  # 「类型 + 方括号」表示法

                                                  最简单的方法是使用「类型 + 方括号」来表示数组:

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

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

                                                  let fibonacci: number[] = [1, '1', 2, 3, 5];
                                                  
                                                  // Type 'string' is not assignable to type 'number'.
                                                  
                                                  1
                                                  2
                                                  3

                                                  数组的一些方法的参数也会根据数组在定义时约定的类型进行限制:

                                                  let fibonacci: number[] = [1, 1, 2, 3, 5];
                                                  fibonacci.push('8');
                                                  
                                                  // Argument of type '"8"' is not assignable to parameter of type 'number'.
                                                  
                                                  1
                                                  2
                                                  3
                                                  4

                                                  上例中,push 方法只允许传入 number 类型的参数,但是却传了一个 "8" 类型的参数,所以报错了。这里 "8" 是一个字符串字面量类型,会在后续章节中详细介绍。

                                                  # 数组泛型

                                                  我们也可以使用数组泛型(Array Generic) Array<elemType> 来表示数组:

                                                  let fibonacci: Array<number> = [1, 1, 2, 3, 5];
                                                  
                                                  1

                                                  关于泛型,可以参考泛型一章。

                                                  # 用接口表示数组

                                                  接口也可以用来描述数组:

                                                  interface NumberArray {
                                                      [index: number]: number;
                                                  }
                                                  let fibonacci: NumberArray = [1, 1, 2, 3, 5];
                                                  
                                                  1
                                                  2
                                                  3
                                                  4

                                                  NumberArray 表示:只要索引的类型是数字时,那么值的类型必须是数字。

                                                  虽然接口也可以用来描述数组,但是我们一般不会这么做,因为这种方式比前两种方式复杂多了。

                                                  不过有一种情况例外,那就是它常用来表示类数组。

                                                  # 类数组

                                                  类数组(Array-like Object)不是数组类型,比如 arguments:

                                                  function sum() {
                                                      let args: number[] = arguments;
                                                  }
                                                  
                                                  // Type 'IArguments' is missing the following properties from type 'number[]': pop, push, concat, join, and 24 more.
                                                  
                                                  1
                                                  2
                                                  3
                                                  4
                                                  5

                                                  上例中,arguments 实际上是一个类数组,不能用普通的数组的方式来描述,而应该用接口:

                                                  function sum() {
                                                      let args: {
                                                          [index: number]: number;
                                                          length: number;
                                                          callee: Function;
                                                      } = arguments;
                                                  }
                                                  
                                                  1
                                                  2
                                                  3
                                                  4
                                                  5
                                                  6
                                                  7

                                                  在这个例子中,我们除了约束当索引的类型是数字时,值的类型必须是数字之外,也约束了它还有 length 和 callee 两个属性。

                                                  事实上常用的类数组都有自己的接口定义,如 IArguments, NodeList, HTMLCollection 等:

                                                  function sum() {
                                                      let args: IArguments = arguments;
                                                  }
                                                  
                                                  1
                                                  2
                                                  3

                                                  其中 IArguments 是 TypeScript 中定义好了的类型,它实际上就是:

                                                  interface IArguments {
                                                      [index: number]: any;
                                                      length: number;
                                                      callee: Function;
                                                  }
                                                  
                                                  1
                                                  2
                                                  3
                                                  4
                                                  5

                                                  关于内置对象,可以参考内置对象一章。

                                                  # any 在数组中的应用

                                                  一个比较常见的做法是,用 any 表示数组中允许出现任意类型:

                                                  let list: any[] = ['xcatliu', 25, { website: 'http://xcatliu.com' }];
                                                  
                                                  1

                                                  # 参考

                                                  • Basic Types # Arrayopen in new window(中文版open in new window)
                                                  • Interfaces # Indexable Typesopen in new window(中文版open in new window)
                                                  edit icon编辑此页open in new window
                                                  上一页
                                                  对象的类型——接口
                                                  下一页
                                                  函数的类型
                                                  傻瓜都能写出计算机可以理解的代码。唯有能写出人类容易理解的代码的,才是优秀的程序员。
                                                  Copyright © 2022 YY

                                                  该应用可以安装在您的 PC 或移动设备上。这将使该 Web 应用程序外观和行为与其他应用程序相同。它将在出现在应用程序列表中,并可以固定到主屏幕,开始菜单或任务栏。此 Web 应用程序还将能够与其他应用程序和您的操作系统安全地进行交互。

                                                  详情