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) 树是计算机数据结构里的专业术语.就像一个学校有很多年级,每个年级下 ...
随机推荐
- 选择排序之C++实现
选择排序之C++实现 一.源代码:SelectSort.cpp /* 选择排序(从小到大)的基本思想是,首先,选出最小的数,放在第一个位置: 然后,选出第二小的数,放在第二个位置: 以此类推,直到所有 ...
- [CC-SEINC]Sereja and Subsegment Increasings
[CC-SEINC]Sereja and Subsegment Increasings 题目大意: 有长度为\(n(n\le10^5)\)的序列\(A\)和\(B\). 在一次操作中,可以选择一个区间 ...
- bzoj 4034
我写的是 DFS序+线段树 DFS序(出去的位置要单独建点)上,进入的位置是权值,出去的位置是权值的相反数,可以证明节点i到根节点的路径上的点的权值和是DFS序上1-in[i]的和. 只要搞出每个区间 ...
- Codeforces Round #357 (Div. 2) B. Economy Game 水题
B. Economy Game 题目连接: http://www.codeforces.com/contest/681/problem/B Description Kolya is developin ...
- HDU 5692 Snacks bfs版本dfs序 线段树
Snacks 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5692 Description 百度科技园内有n个零食机,零食机之间通过n−1条路相互连 ...
- CSS选择器复习
通用选择器:* 选择到所有的元素 选择子元素:> 选择到元素的直接后代(第一级子元素) 相邻兄弟选择器:+ 选择到紧随目标元素后的第一个元素 普通兄弟选择器:~ 选择到紧随其后的所有兄弟元素 伪 ...
- SlickSafe.NET 开源权限框架开发指南
前言:本文适用于快速搭建权限系统的用户,尤其适用于希望有良好定义的权限模型建立:系统解决方案是在基于角色访问控制(RBAC)策略基础上的权限访问模型实现,主要完成了后台权限验证逻辑和前端权限数据验证的 ...
- [Database] Redis 随笔
Redis 随笔 1. 特点 非关系数据库 non-relational database 内存数据库 高性能 主从复制 可持久化存储 发布与订阅 支持脚本 2. 数据类型5种 STRING 可以是字 ...
- 【scrapy】使用方法概要(一)(转)
[请初学者作为参考,不建议高手看这个浪费时间] 工作中经常会有这种需求,需要抓取互联网上的数据.笔者就经常遇到这种需求,一般情况下会临时写个抓取程序,但是每次遇到这种需求的时候,都几乎要重头写,特别是 ...
- BeeProg2C Extremely fast universal USB interfaced programmer
http://www.elnec.com/products/universal-programmers/beeprog2c/ FPGA based totally reconfigurable 48 ...