首页 > 其他 > 详细

Typescript declaration: Merge a class and an interface

时间:2018-11-08 17:49:26      阅读:210      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!