Typescript declaration: Merge a class and an interface
参考: https://stackoverflow.com/questions/47670959/typescript-declaration-merge-a-class-and-an-interface
--------------------------------------------------------
extend a enumeration with a method:
https://blog.oio.de/2014/03/21/declaration-merging-typescript/
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];
}
}
console.log(UserType.parse('0'));
interface Box {
height: number;
width: number;
}
//----------------------------------------------
interface ClientModel extends Box{
}
// interface ClientModel extends Box { }
class ClientModel{
public say():string{
console.log(this.height);
return '123421';
}
}
let a = new ClientModel();
a.height = 123;
console.log(a.say());
--------------------------------------------------------
ou can use declaration merging. If the class and the interface are declared in the same namespace/module and have the same name, they will be merged into a single class type.
interface ClientModel {
name: string;
email: string;
}
class ClientModel extends Model {
m() {
this.email //Valid
}
}
If you cannot change the interface or is declared in another namespace and you can't move it you can inherit from it in the merged interface:
interface Client {
name: string;
email: string;
}
interface ClientModel extends Client {}
class ClientModel extends Model {
m() {
this.email //Valid
}
}
Typescript declaration: Merge a class and an interface的更多相关文章
- TypeScript Declaration Merging(声明合并)
TypeScript中有一些独特的概念,来自需要描述JavaScript对象类型发生了哪些变化.举个例子,最为独特的概念就是"声明合并".理解了这个概念将会对你在当前JavaScr ...
- TypeScript Interface(接口)
类型检查专注于解析值所具有的"形态",这是TypeScript的核心原则之一.这个有时候被称为"duck typing"或者"structural s ...
- 探索typescript的必经之路-----接口(interface)
TypeScript定义接口 熟悉编程语言的同学都知道,接口(interface)的重要性不言而喻. 很多内容都会运用到接口.typescrip中的接口类似于java,同时还增加了更灵活的接口类型,包 ...
- TypeScript之路----探索接口(interface)的奥秘
TypeScript定义接口 要想掌握typescript的知识,接口是其必经之路.很多东西都需要接触到接口,接口除了对类的一部分行为进行抽象以外,也常用于对对象的形状进行描述.接下来我们就一起来学习 ...
- TypeScript Type Compatibility(类型兼容)
TypeScript中的类型兼容是基于结构归类的.在普通分类的相比之下,结构归类是一种纯粹用于将其成员的类型进行关联的方法.思考下面的代码: interface Named { name: strin ...
- TypeScript 零基础入门
前言 2015 年末看过一篇文章<ES2015 & babel 实战:开发 npm 模块>,那时刚接触 ES6 不久,发觉新的 ES6 语法大大简化了 JavaScript 程序的 ...
- TypeScript 版本相关
TypeScript 1.3 元组类型 // Declare a tuple type var x: [string, number]; // 初始化 x = []; // ok // 错误的初始化 ...
- 10 Essential TypeScript Tips And Tricks For Angular Devs
原文: https://www.sitepoint.com/10-essential-typescript-tips-tricks-angular/ ------------------------- ...
- TypeScript 源码详细解读(4)语法1-语法树
在上一节介绍了标记的解析,就相当于识别了一句话里有哪些词语,接下来就是把这些词语组成完整的句子,即拼装标记为语法树. 树(tree) 树是计算机数据结构里的专业术语.就像一个学校有很多年级,每个年级下 ...
随机推荐
- 富文本插件KindEditor
具体用法查看官网http://kindeditor.net/doc.php {% load staticfiles %} <!DOCTYPE html> <html lang=&qu ...
- android 消息机制,handler机制,messageQueue,looper
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha handler 就是 处理器 . 用来处理消息, 发送消息. handler 就 ...
- shellcode在栈溢出中的利用与优化
0x00 前言 在<Windows Shellcode学习笔记——shellcode的提取与测试>中介绍了如何对shellcode作初步优化,动态获取Windows API地址并调用,并通 ...
- java集合之二(collection架构)
转载请注明出处:http://www.cnblogs.com/skywang12345/p/3308513.html 首先,我们对Collection进行说明.下面先看看Collection的一些框架 ...
- opencv第二课 进行缩放图片~
#include<stdio.h> #include<iostream> #include<opencv2\opencv.hpp> using namespace ...
- git一些命令
************git基本命令***************git config --global user.name "xielehe" 设置名字git config - ...
- POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14902 Accept ...
- 关于Google Android平台的ClockworkMod Recovery恢复模式
lockworkMod Recovery,它也被称为Clockwork与CWM,它是装载Google Android操作系统设备的一个自定义的Recovery恢复模式,它可以使得相关Android设备 ...
- strace_for_windows
http://drmemory.org/strace_for_windows.html https://github.com/intellectualheaven/stracent/releases
- Oracle约束的启用和停用
关于Oracle的约束概念和基本操作,我已经在以前的<Constraint基础概念>.<Constraint的简单操作>两篇文章中有过比较详细的介绍了,但是对于如何停用和启 ...