码迷,mamicode.com
首页 > 其他好文 > 详细

Typescript declaration: Merge a class and an interface

时间:2018-11-08 18:28:14      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:ace   tps   highlight   red   space   ber   esc   ali   undefined   

参考: 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

标签:ace   tps   highlight   red   space   ber   esc   ali   undefined   

原文地址:https://www.cnblogs.com/oxspirt/p/9930014.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!