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