参考: 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的更多相关文章

  1. TypeScript Declaration Merging(声明合并)

    TypeScript中有一些独特的概念,来自需要描述JavaScript对象类型发生了哪些变化.举个例子,最为独特的概念就是"声明合并".理解了这个概念将会对你在当前JavaScr ...

  2. TypeScript Interface(接口)

    类型检查专注于解析值所具有的"形态",这是TypeScript的核心原则之一.这个有时候被称为"duck typing"或者"structural s ...

  3. 探索typescript的必经之路-----接口(interface)

    TypeScript定义接口 熟悉编程语言的同学都知道,接口(interface)的重要性不言而喻. 很多内容都会运用到接口.typescrip中的接口类似于java,同时还增加了更灵活的接口类型,包 ...

  4. TypeScript之路----探索接口(interface)的奥秘

    TypeScript定义接口 要想掌握typescript的知识,接口是其必经之路.很多东西都需要接触到接口,接口除了对类的一部分行为进行抽象以外,也常用于对对象的形状进行描述.接下来我们就一起来学习 ...

  5. TypeScript Type Compatibility(类型兼容)

    TypeScript中的类型兼容是基于结构归类的.在普通分类的相比之下,结构归类是一种纯粹用于将其成员的类型进行关联的方法.思考下面的代码: interface Named { name: strin ...

  6. TypeScript 零基础入门

    前言 2015 年末看过一篇文章<ES2015 & babel 实战:开发 npm 模块>,那时刚接触 ES6 不久,发觉新的 ES6 语法大大简化了 JavaScript 程序的 ...

  7. TypeScript 版本相关

    TypeScript 1.3 元组类型 // Declare a tuple type var x: [string, number]; // 初始化 x = []; // ok // 错误的初始化 ...

  8. 10 Essential TypeScript Tips And Tricks For Angular Devs

    原文: https://www.sitepoint.com/10-essential-typescript-tips-tricks-angular/ ------------------------- ...

  9. TypeScript 源码详细解读(4)语法1-语法树

    在上一节介绍了标记的解析,就相当于识别了一句话里有哪些词语,接下来就是把这些词语组成完整的句子,即拼装标记为语法树. 树(tree) 树是计算机数据结构里的专业术语.就像一个学校有很多年级,每个年级下 ...

随机推荐

  1. dSploitzANTI渗透教程之修改MAC地址与Wifi监听器

    dSploitzANTI渗透教程之修改MAC地址与Wifi监听器 dSploitzANTI基本配置 渗透测试是一种安全性较大的工作.所以,在实施渗透测试之前进行一些简单设置.如修改MAC地址.了解网络 ...

  2. redis 客户端命令

    Redis 通过监听一个 TCP 端口或者 Unix socket 的方式来接收来自客户端的连接 1 .CLIENT LIST 返回连接到 redis 服务的客户端列表 2 .CLIENT SETNA ...

  3. php异步实现,避免长时间等待

    处理的php异步的方法有好几种,这里我就只介绍我经常用的而且官方也推荐的 废话少说,直接贴代码 //php异步 public function doRequest($host,$path, $para ...

  4. oracle复杂查询是sql

    一.over()分析函数 分组查前几条:select * from test t where (select count(*) from test a where t.type=a.type and ...

  5. Codeforces Round #350 (Div. 2) D1. Magic Powder - 1 二分

    D1. Magic Powder - 1 题目连接: http://www.codeforces.com/contest/670/problem/D1 Description This problem ...

  6. 封装scroll.js 获取滚动条的值

    function Obj(){} Obj.prototype={ scroll:function(){ /* 主要是做兼容处理 这里必须时!=null 因为默认值和每次滚动的时侯 都可以值为0 但是 ...

  7. SyncthingTray -- Syncthing wrapper for Windows

    SyncthingTray Syncthing wrapper for Windows. Includes a small interface to configure start on boot a ...

  8. winform打开进程与关闭进程

    #region 判断某进程名是否运行 /// <summary> /// 关闭指定名称的进程 /// </summary> /// <param name="p ...

  9. datagrid在MVC中的运用03-选择单行或多行

    本文体验datagrid显示单行或多行内容.分别用到了datagrid的getSelected,getSelections方法. Html部分 <a href="#" cla ...

  10. 使用jdbc的缺点

    使用jdbc开发时有以下缺点 1,数据库连接,使用时就创建,不使用就释放,对数据库进行频繁连接开关和关闭,造成数据库资源浪费,影响数据库的性能 解决:使用数据库连接池管理数据库的连接 2,sql语句使 ...