【TypeScript】013-声明合并、扩展阅读

8、声明合并

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

函数的合并

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

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;
}

类的合并

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

9、扩展阅读

此处记录了官方手册中文版)中包含,但是本书未涉及的概念。

我认为它们是一些不重要或者不属于 TypeScript 的概念,所以这里只给出一个简单的释义,详细内容可以点击链接深入理解。

  • Never([中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Basic Types.html#never)):永远不存在值的类型,一般用于错误处理函数
  • Variable Declarations([中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Variable Declarations.html)):使用 letconst 替代 var,这是 ES6 的知识
  • this:箭头函数的运用,这是 ES6 的知识
  • Using Class Types in Generics中文版):创建工厂函数时,需要引用构造函数的类类型
  • Best common type([中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Type Inference.html#最佳通用类型)):数组的类型推论
  • Contextual Type([中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Type Inference.html#上下文类型)):函数输入的类型推论
  • Type Compatibility([中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Type Compatibility.html)):允许不严格符合类型,只需要在一定规则下兼容即可
  • Advanced Types([中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Advanced Types.html#交叉类型(intersection-types))):使用 & 将多种类型的共有部分叠加成一种类型
  • Type Guards and Differentiating Types([中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Advanced Types.html#类型保护与区分类型(type-guards-and-differentiating-types))):联合类型在一些情况下被识别为特定的类型
  • Discriminated Unions([中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Advanced Types.html#可辨识联合(discriminated-unions))):使用 | 联合多个接口的时候,通过一个共有的属性形成可辨识联合
  • Polymorphic this types([中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Advanced Types.html#多态的this类型)):父类的某个方法返回 this,当子类继承父类后,子类的实例调用此方法,返回的 this 能够被 TypeScript 正确的识别为子类的实例。
  • Symbols中文版):新原生类型,这是 ES6 的知识
  • Iterators and Generators([中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Iterators and Generators.html)):迭代器,这是 ES6 的知识
  • Namespaces中文版):避免全局污染,现在已被 ES6 Module 替代
  • Decorators中文版):修饰器,这是 ES7 的一个提案
  • Mixins中文版):一种编程模式,与 TypeScript 没有直接关系,可以参考 ES6 中 Mixin 模式的实现
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值