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

    • 原始数据类型
      • 任意值
        • 类型推论
          • 联合类型
            • 对象的类型——接口
              • 数组的类型
                • 函数的类型
                  • 类型断言
                    • 声明文件
                      • 内置对象
                      • Typescript进阶

                        • 类型别名
                          • 字符串字面量类型
                            • 元组
                              • 简单的例子
                                • 越界的元素
                                  • 参考
                                  • 枚举
                                    • 类
                                      • 类与接口
                                        • 泛型
                                          • 声明合并
                                            • 扩展阅读

                                            元组

                                            author iconYYtimer icon大约 1 分钟

                                            此页内容
                                            • 简单的例子
                                            • 越界的元素
                                            • 参考

                                            # 元组

                                            数组合并了相同类型的对象,而元组(Tuple)合并了不同类型的对象。

                                            元组起源于函数编程语言(如 F#),这些语言中会频繁使用元组。

                                            # 简单的例子

                                            定义一对值分别为 string 和 number 的元组:

                                            let tom: [string, number] = ['Tom', 25];
                                            
                                            1

                                            当赋值或访问一个已知索引的元素时,会得到正确的类型:

                                            let tom: [string, number];
                                            tom[0] = 'Tom';
                                            tom[1] = 25;
                                            
                                            tom[0].slice(1);
                                            tom[1].toFixed(2);
                                            
                                            1
                                            2
                                            3
                                            4
                                            5
                                            6

                                            也可以只赋值其中一项:

                                            let tom: [string, number];
                                            tom[0] = 'Tom';
                                            
                                            1
                                            2

                                            但是当直接对元组类型的变量进行初始化或者赋值的时候,需要提供所有元组类型中指定的项。

                                            let tom: [string, number];
                                            tom = ['Tom', 25];
                                            
                                            1
                                            2
                                            let tom: [string, number];
                                            tom = ['Tom'];
                                            
                                            // Property '1' is missing in type '[string]' but required in type '[string, number]'.
                                            
                                            1
                                            2
                                            3
                                            4

                                            # 越界的元素

                                            当添加越界的元素时,它的类型会被限制为元组中每个类型的联合类型:

                                            let tom: [string, number];
                                            tom = ['Tom', 25];
                                            tom.push('male');
                                            tom.push(true);
                                            
                                            // Argument of type 'true' is not assignable to parameter of type 'string | number'.
                                            
                                            1
                                            2
                                            3
                                            4
                                            5
                                            6

                                            # 参考

                                            • Basic Types # Tupleopen in new window(中文版open in new window)
                                            edit icon编辑此页open in new window
                                            上一页
                                            字符串字面量类型
                                            下一页
                                            枚举
                                            傻瓜都能写出计算机可以理解的代码。唯有能写出人类容易理解的代码的,才是优秀的程序员。
                                            Copyright © 2022 YY

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

                                            详情