zoukankan      html  css  js  c++  java
  • TypeScript-初级-08-声明合并

    声明合并

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

    函数的合并

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

    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('');
        }
    }
    

    接口的合并

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

    interface Alarm {
        price: number;
    }
    interface Alarm {
        weight: number;
    }
    

    相当于:

    interface Alarm {
        price: number;
        weight: number;
    }
    

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

    interface Alarm {
        price: number;
    }
    interface Alarm {
        price: number;  // 虽然重复了,但是类型都是 `number`,所以不会报错
        weight: number;
    }
    
    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'.
    

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

    interface Alarm {
        price: number;
        alert(s: string): string;
    }
    interface Alarm {
        weight: number;
        alert(s: string, n: number): string;
    }
    

    相当于:

    interface Alarm {
        price: number;
        weight: number;
        alert(s: string): string;
        alert(s: string, n: number): string;
    }
    

    类的合并

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

  • 相关阅读:
    笔试助攻题(思路)
    const 修饰成员函数 前后用法(effective c++ 03)
    UNIX 是啥?!和Linux什么关系?
    我的offer之路(一)
    我的offer之路(一)
    ANSI C 与 K&R C
    c内置数据类型
    预处理器
    小数用二进制如何表示
    C++中有三种创建对象的方法
  • 原文地址:https://www.cnblogs.com/idspring/p/11784742.html
Copyright © 2011-2022 走看看