原文: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. Problem B: 七龙珠II

    Description 小王去找了个算命先生算算这辈子是有钱还是没钱.他在纸上写下“性命”两个字,问哪个字重要. 小王想了想说当然是命比较重要. 他摇摇头:“你,没钱” “为什么?” “有钱,任性.没 ...

  2. BZOJ3522&4543 [POI2014]Hotel加强版 长链剖分

    上上周见fc爷用长链剖分秒题 于是偷偷学一学 3522的数据范围很小 可以暴力枚举每个点作为根节点来dp 复杂度$O(n^2)$ 考虑令$f[x][j]$表示以$x$为根的子树内距离$x$为$j$的点 ...

  3. 【对比分析六】JavaScript中GET和POST的区别及使用场景

    区别: GET:一般用于信息获取,使用URL传递参数,对所发送信息的数量也有限制,一般在2000个字符 POST:一般用于修改服务器上的资源,对所发送的信息没有限制 GET方式需要使用 Request ...

  4. hdu 4163 Stock Prices 花式排序

    Stock Prices Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. wikioi 1080 线段树练习 树状数组

    1080 线段树练习 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond       题目描述 Description 一行N个方格,开始每个格子里都有一个整数.现 ...

  6. Java线程锁&分布式锁的理解及应用

    了解Java线程锁之前,先理解线程和进程的定义.进程是操作系统分配资源(CPU)的基本单位,线程是CPU执行的基本单位,一个进程可拥有多个线程,同进程间的多个线程共享分配给进程的资源.比如启动JVM时 ...

  7. flash 跨域加载 二次加载

    var url2:String = "http://thirdapp0.qlogo.cn/qzopenapp/fa5d80ebf9fc89aaa1d7ddb0e1b861e58d77b409 ...

  8. spring对事务支持的三种形式

    spring对事务支持的三种形式: 1.通过spring配置文件进行切面配置 <bean id="***Manager" class="org.springfram ...

  9. WordPress 客户端软件列表

    Windows: BlogDesk BlogJet Blog Writer Chrysanth WebStory Deepest Sender (Firefox或SeaMonkey扩展,跨平台- De ...

  10. 安装oracle 11gr2 rac on solaris

    http://candon123.blog.51cto.com/704299/389470