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

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

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

                                              声明合并

                                              author iconYYtimer icon大约 1 分钟

                                              此页内容
                                              • 函数的合并
                                              • 接口的合并
                                              • 类的合并
                                              • 参考

                                              # 声明合并

                                              如果定义了两个相同名字的函数、接口或类,那么它们会合并成一个类型:

                                              # 函数的合并

                                              之前学习过,我们可以使用重载定义多个函数类型:

                                              function reverse(x: number): number;
                                              function reverse(x: string): string;
                                              function reverse(x: number | string): number | string {
                                                  if (typeof x === 'number') {
                                                      return Number(x.toString().split('').reverse().join(''));
                                                  } else if (typeof x === 'string') {
                                                      return x.split('').reverse().join('');
                                                  }
                                              }
                                              
                                              1
                                              2
                                              3
                                              4
                                              5
                                              6
                                              7
                                              8
                                              9

                                              # 接口的合并

                                              接口中的属性在合并时会简单的合并到一个接口中:

                                              interface Alarm {
                                                  price: number;
                                              }
                                              interface Alarm {
                                                  weight: number;
                                              }
                                              
                                              1
                                              2
                                              3
                                              4
                                              5
                                              6

                                              相当于:

                                              interface Alarm {
                                                  price: number;
                                                  weight: number;
                                              }
                                              
                                              1
                                              2
                                              3
                                              4

                                              注意,合并的属性的类型必须是唯一的:

                                              interface Alarm {
                                                  price: number;
                                              }
                                              interface Alarm {
                                                  price: number;  // 虽然重复了,但是类型都是 `number`,所以不会报错
                                                  weight: number;
                                              }
                                              
                                              1
                                              2
                                              3
                                              4
                                              5
                                              6
                                              7
                                              interface Alarm {
                                                  price: number;
                                              }
                                              interface Alarm {
                                                  price: string;  // 类型不一致,会报错
                                                  weight: number;
                                              }
                                              
                                              // index.ts(5,3): error TS2403: Subsequent variable declarations must have the same type.  Variable 'price' must be of type 'number', but here has type 'string'.
                                              
                                              1
                                              2
                                              3
                                              4
                                              5
                                              6
                                              7
                                              8
                                              9

                                              接口中方法的合并,与函数的合并一样:

                                              interface Alarm {
                                                  price: number;
                                                  alert(s: string): string;
                                              }
                                              interface Alarm {
                                                  weight: number;
                                                  alert(s: string, n: number): string;
                                              }
                                              
                                              1
                                              2
                                              3
                                              4
                                              5
                                              6
                                              7
                                              8

                                              相当于:

                                              interface Alarm {
                                                  price: number;
                                                  weight: number;
                                                  alert(s: string): string;
                                                  alert(s: string, n: number): string;
                                              }
                                              
                                              1
                                              2
                                              3
                                              4
                                              5
                                              6

                                              # 类的合并

                                              类的合并与接口的合并规则一致。

                                              # 参考

                                              • Declaration Mergingopen in new window(中文版open in new window)
                                              edit icon编辑此页open in new window
                                              上一页
                                              泛型
                                              下一页
                                              扩展阅读
                                              傻瓜都能写出计算机可以理解的代码。唯有能写出人类容易理解的代码的,才是优秀的程序员。
                                              Copyright © 2022 YY

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

                                              详情