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 ...
随机推荐
- 用 Python 实现一个大数据搜索引擎
搜索是大数据领域里常见的需求.Splunk和ELK分别是该领域在非开源和开源领域里的领导者.本文利用很少的Python代码实现了一个基本的数据搜索功能,试图让大家理解大数据搜索的基本原理. 布隆过滤器 ...
- luoguP4036 [JSOI2008]火星人 平衡树+hash
这个操作十分的复杂 但是可以拿平衡树维护 直接二分答案然后用$hash$值判断即可 复杂度$O(10000 * log^2 n + n \log n)$ #include <cstdio> ...
- VC被控制时关闭极域电子教室、破解联想硬盘保护系统密码(上)
<[原]关于VC运行时关闭极域电子教室的改进方法> 本文将讲资料和方法,具体实现和破解联想硬盘保护系统密码在(下)中,有关破解联想硬盘保护系统(删除它)的方法很简单,用硬盘保护卡克星就可以 ...
- hdu 4403 爆搜
题意:给一串数字,在其间加入若干加号和一个等号,问使等式成立的方案总数 if the digits serial is "1212", you can get 2 equation ...
- 【POJ】2796:Feel Good【单调栈】
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 18449 Accepted: 5125 Case T ...
- Codeforces Round #296 (Div. 1) A. Glass Carving Set的妙用
A. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- jProfiler远程连接Linux监控jvm1运行状态
第一步:下载软件官网地址:https://www.ej-technologies.com/download/jprofiler/files,下载一个linux服务端,一个windows客户端 GUI界 ...
- Bootstrap_表格
Bootstrap 表格 Bootstrap 提供了一个清晰的创建表格的布局.下表列出了 Bootstrap 支持的一些表格元素: 标签 描述 <table> 为表格添加基础样式. < ...
- html5调用手机本地摄像头和相册识别二维码详细实现过程
项目中有用到h5识别我们的单据,单据上面有二维码. 实现的场景就是业务人员扫码 类似以下场景 业务员拿到单据以后,直接可以扫码进入相关单据业也可以 输入二维码下方的号码进行识别 下面是h5的页面构造 ...
- UVa11187
莫勒定理,证明如下: 请结合下图看代码: #include <iostream> #include <math.h> #include <iomanip> usin ...