Declaration Merging with TypeScript
原文: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的更多相关文章
- TypeScript Declaration Merging(声明合并)
TypeScript中有一些独特的概念,来自需要描述JavaScript对象类型发生了哪些变化.举个例子,最为独特的概念就是"声明合并".理解了这个概念将会对你在当前JavaScr ...
- Typescript declaration: Merge a class and an interface
参考: https://stackoverflow.com/questions/47670959/typescript-declaration-merge-a-class-and-an-interfa ...
- TypeScript Type Compatibility(类型兼容)
TypeScript中的类型兼容是基于结构归类的.在普通分类的相比之下,结构归类是一种纯粹用于将其成员的类型进行关联的方法.思考下面的代码: interface Named { name: strin ...
- TypeScript & JavaScript
http://www.typescriptlang.org/docs/tutorial.html handbook: Basic Types Variable Declarations Interfa ...
- TypeScript: type alias 与 interface
官方文档中有关于两者对比的信息,隐藏在 TypeScript Handbook 中,见 Interfaces vs. Type Aliases 部分. 但因为这一部分很久没更新了,所以其中描述的内容不 ...
- 【区分】Typescript 中 interface 和 type
在接触 ts 相关代码的过程中,总能看到 interface 和 type 的身影.只记得,曾经遇到 type 时不懂查阅过,记得他们很像,相同的功能用哪一个都可以实现.但最近总看到他们,就想深入的了 ...
- TypeScript开发手册
返回TS学习总目录 基本类型(Basic Types) 接口(Interfaces) 类(Classes) 模块(Modules) 函数(Functions) 泛型(Generics) 常见错误(Co ...
- react typescript jest config (一)
1. initialize project create a folder project Now we'll turn this folder into an npm package. npm in ...
- react: typescript project initialize
Initialize the project create a folder project Now we’ll turn this folder into an npm package. npm i ...
随机推荐
- 某谷 P5159 WD与矩阵
题面在这里 崴脚回家后的小休闲2333. 显然每一行的1的个数必须是偶数,这样可以归纳证明前i行异或出来的m位二进制数也有偶数个1,这样最后一行就有且仅有一种放法了. 于是ans = 2^((n-1) ...
- PHP5.3以上版本使用pthreads PHP扩展真正支持多线程
class test_thread_run extends Thread{public $url;public $data; public function __construct($url){$th ...
- bzoj 2850
比较基础的KD树.每个节点维护一个BOX,包含包含当当前子树的点的最小矩形,以及点权和,然后用“整个矩形都在直线的一侧”和“整个矩形都不在直线的一侧”剪枝. /******************** ...
- Codeforces Round #257 (Div. 2 ) B. Jzzhu and Sequences
B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- MYSQL-5.5.37-win32.msi 这个版本得程序包谁有吗 可以给我一下吗?
之前下载了这个版本得mysql 但是跟服务器链接不上 后来我就卸载了 但由于卸载不干净 现在又删了注册表 好像把这个程序包得什么文件删除了 现在提示配置文件错误 所以有这个程序包得 ...
- spring---aop(4)---Spring AOP的CGLIB动态代理
写在前面 前面介绍了Spring AOP的JDK动态代理的过程,这一篇文章就要介绍下Spring AOP的Cglib代理过程. CGLib全称为Code Generation Library,是一个强 ...
- 华为S5300系列交换机V200R001SPH027升级补丁
S5300SI-V200R001SPH027.pat 附件: 链接:https://pan.baidu.com/s/1ulE0j5Rp4xMkAaOjNGKLMA 密码:d5ze
- mt7620 wireless驱动特性意外发现
前言 今天又客户反映无线參数SSID编程了HT_AP0, 同事通过后台给他改动后反映给我,我想不正确啊,难道是无线驱动crash了?那应该不能玩才对啊... 追查线索 我们的路由器会定期汇报数据SSI ...
- [Asp.net web api]缓存
摘要 为了提高接口的性能,我们常做的优化就包括缓存,对经常访问但变化不大的数据进行缓存.或者使用http的缓存,减少请求的次数. web api缓存 在提供的api,我们也可以实现缓存,来减少访问的次 ...
- Redis持久化RDB和AOF优缺点是什么,怎么实现的?我应该用哪一个?
原文http://www.ymq.io/2018/03/24/redis/ Redis是一种高级key-value数据库.数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支 ...