原文:https://blog.oio.de/2014/03/21/declaration-merging-typescript/

Why might you need this?

There can be several scenarios where this might be required. One of the most common ones is when you want to extend an existing JavaScript library that comes with a type definition.

---------------------------------------------------

Declaration Merging with TypeScript

Declaration merging is a very interesting feature of TypeScript, the statically typed superset of JavaScript.
As you will see, different things can be merged in TypeScript.
The merging is always based on matching names, so as soon as two e.g. interfaces have the same name (and live in the same namespace), they will get merged.

What can be merged in TypeScript?

In TypeScript it is possible two merge
– mutliple interfaces
– multiple modules
– modules with classes
– modules with functions
– modules with enums

Merging multiple interfaces

To merge multiple interfaces, simply give them the same name.

1
2
3
4
5
6
7
8
interface Foo {
    doIt();
}
 
interface Foo {
    doSomething();
    doSomethingDifferent();
}

This will result in a merged interface as follows.

1
2
3
4
5
interface Foo {
    doSomething();
    doSomethingDifferent();
    doIt();
}

As you can see, the two interfaces are merged in reverse order, but the order of the declarations in each individual interface is not changed.
A reverse merge order is important if you want to extend a library.

Merging multiple modules

Modules with the same name will merge their members, effectively creating a common namespace.

1
2
3
4
5
6
7
module mod {
    export class Foo { }
}
 
module mod {
    export class Bar extends Foo { }
}

Merging modules is a common task if you use internal modules with TypeScript. It enables you two use the one class per file best practice while placing multiple classes inside the same namespace.

If you have a Java background, merging modules can be compared to putting multiple classes inside the same package.

Merging modules with classes, functions and enums

You can merge modules with classes, functions and enums. This might sound strange, but it allows you to extend these constructs with additional properties in a typesafe way.

Here is an example on how to extend a function with properties, which is a common practice in the JavaScript world:

1
2
3
4
5
6
7
function greet() {
    console.log("Hello " + greet.target);
}
 
module greet {
    export var target = "World";
}

Here is another example that extends an enum with a method:

1
2
3
4
5
6
7
8
9
10
11
12
13
enum UserType {
    ADMIN, USER, GUEST
}
 
module UserType {
    export function parse(value: string): UserType {
        var UT: any = UserType;
        if(typeof UserType[value] === "undefined") {
            throw new Error("unknown value of enum UserType: " + value);
        }
        return UserType[value];
    }
}

As you can see in another blog post, merging a class with a module containing another class can be used to create inner classes.

What cannot be merged in TypeScript?

In the current TypeScript version (1.0RC at the time of writing), it is not possible to merge the following:
– multiple classes
– classes with variables
– classes with interfaces

This may change in future TypeScript versions.
Mixins could be an alternative approach for these things.

For additional information, take a look at the wiki page at Codeplex.

Declaration Merging with TypeScript的更多相关文章

  1. TypeScript Declaration Merging(声明合并)

    TypeScript中有一些独特的概念,来自需要描述JavaScript对象类型发生了哪些变化.举个例子,最为独特的概念就是"声明合并".理解了这个概念将会对你在当前JavaScr ...

  2. Typescript declaration: Merge a class and an interface

    参考: https://stackoverflow.com/questions/47670959/typescript-declaration-merge-a-class-and-an-interfa ...

  3. TypeScript Type Compatibility(类型兼容)

    TypeScript中的类型兼容是基于结构归类的.在普通分类的相比之下,结构归类是一种纯粹用于将其成员的类型进行关联的方法.思考下面的代码: interface Named { name: strin ...

  4. TypeScript & JavaScript

    http://www.typescriptlang.org/docs/tutorial.html handbook: Basic Types Variable Declarations Interfa ...

  5. TypeScript: type alias 与 interface

    官方文档中有关于两者对比的信息,隐藏在 TypeScript Handbook 中,见 Interfaces vs. Type Aliases 部分. 但因为这一部分很久没更新了,所以其中描述的内容不 ...

  6. 【区分】Typescript 中 interface 和 type

    在接触 ts 相关代码的过程中,总能看到 interface 和 type 的身影.只记得,曾经遇到 type 时不懂查阅过,记得他们很像,相同的功能用哪一个都可以实现.但最近总看到他们,就想深入的了 ...

  7. TypeScript开发手册

    返回TS学习总目录 基本类型(Basic Types) 接口(Interfaces) 类(Classes) 模块(Modules) 函数(Functions) 泛型(Generics) 常见错误(Co ...

  8. react typescript jest config (一)

    1. initialize project create a folder project Now we'll turn this folder into an npm package. npm in ...

  9. react: typescript project initialize

    Initialize the project create a folder project Now we’ll turn this folder into an npm package. npm i ...

随机推荐

  1. webstorm2018最新激活码license server

    2018.7.5最新激活码: license server:https://s.tuzhihao.com:666/ 以后持续更新....

  2. Luogu 4492 [HAOI2018]苹果树 组合数

    https://www.luogu.org/problemnew/show/P4492 找每个编号的点的父边的贡献,组合数和阶乘就能算了. 我考场上怎么就是没想到呢. 调了好久好久好久好久调不出来,样 ...

  3. 网站(Web)压测工具Webbench源码分析

    一.我与webbench二三事 Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能.Webbench ...

  4. bzoj1477 poj1061 青蛙的约会

    Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...

  5. 计算机二级软件VC++6.0下载地址

    计算机二级软件VC++6.0介绍: 适合所有参加全国计算机等级考试的童鞋们……见图如下: 下载地址:(以下两者任选其一即可) (1).计算机二级软件VC++6.0(16.35MB) (2).计算机二级 ...

  6. http状态码学习笔记

    当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求.当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含HTTP状态码的信息头(server header)用以响应浏览器的请求. ...

  7. scriptlet

    <!-- <%! %>:可以修饰全局变量.常量.类.方法 对应java类中的成员变量.常量.内部类.成员方法 --> <%! int num=10;//全局变量 publ ...

  8. 【原】使用Spring自带的JdbcTemplate。

    使用Spring自带的JdbcTemplate,可以简化对数据库的操作,用起来十分方便.通过一下几个步骤的配置,即可以使用JdbcTemplate. (1)配置好Spring的数据源,加入mysql驱 ...

  9. MySQL增强版命令行客户端连接工具(mycli)

    效果: 安装: http://www.mycli.net/install 官网: http://www.mycli.net/install

  10. 云计算大会有感—MapReduce和UDF

    (转载请注明出处:http://blog.csdn.net/buptgshengod) 1.參会有感       首先还是非常感谢CSDN能给我票,让我有机会參加这次中国云计算峰会.感觉不写点什么对不 ...