[英]Typescript declaration: Merge a class and an interface
I have two models Model
and its subclass ClientModel
an ambient module. 我有两个模型Model
和它的子类ClientModel
是一个环境模块。 Now I want to declare a set of attributes of ClientModel
from an interface so called Client
. 现在我想从一个名为Client
的接口声明ClientModel
一组属性。 How can I do it? 我该怎么做? I can imagine something like this: 我可以想象这样的事情:
interface Client {
name: string;
email: string;
}
declare class ClientModel extends Model implements Client {
// with name and email here without redeclare them
}
You 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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.