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) 树是计算机数据结构里的专业术语.就像一个学校有很多年级,每个年级下 ...
随机推荐
- django组件之contenttype(一)
方式1:适用于1张表和另一张表要关联的时候. 1.路飞学成表设计: 2.将2个价格策略表合并1张表. 3.如果再加一张表,那价格策略表的表结构会发生改变. 这样不合理的,我们的表结构一般设计完就不会 ...
- django 动态url 可变
首先在urls里面改,name=让一个映射敷个名字. 然后到books——list页面让编辑按钮改成这种可变的映射模式.
- Java并发(二十一):线程池实现原理
一.总览 线程池类ThreadPoolExecutor的相关类需要先了解: (图片来自:https://javadoop.com/post/java-thread-pool#%E6%80%BB%E8% ...
- mysql反向解析导致连接缓慢
Content 0.序 1.问题 2.原因 3.解决办法 0.序 本文主要是记录Mysql安装在 VMWARE下,本地连接Mysql速度很慢的原因及解决办法. 1.问题 本地的一个网站使用mysql数 ...
- configure: error: lzo enabled but missing
注意:yum安装的无效,需要手动下载源码安装. wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.06.tar.gz tar zx ...
- SyncTrayzor -- Windows tray utility / filesystem watcher / launcher for syncthing
SyncTrayzor is a little tray utility for Syncthing on Windows. It hosts and wraps Syncthing, making ...
- Nginx 作为反向Proxy 的优化要点
原文地址:http://my.oschina.net/hyperichq/blog/405421 常用优化要点 当nginx用于反向代理时,每个客户端将使用两个连接: 一个用于响应客户端的请求,另一个 ...
- OSChina 周日乱弹 —— 超酷炫 58 页年终总结,笑喷!
猿文:http://my.oschina.net/xxiaobian/blog/368981 借鉴一下以下的各种年终总结吧,总有适合你的... OSCer 们,小小编掐指一算,你命里真的缺我啊,赶紧给 ...
- appium+python自动化59-appium命令行参数
Appium服务器参数 许多Appium 1.5服务器参数已被弃用,以支持--default-capabilities标志. 用法: node . [flags] help 1.cmd端口输入,app ...
- SVN中的Trunk、Tag、Brance的用法
在SVN中Branch/tag在一个功能选项中,在使用中也往往产生混淆.在实现上,branch和tag,对于svn都是使用copy实现的,所以他们在默认的权限上和一般的目录没有区别.至于何时用tag, ...