我有以下界面:
interface SMJSPacket {
header: {
tag: string;
method: string;
type: string;
};
response?: {
status: string;
content: string;
};
event?: {
key?: string;
action?: string;
};
request?: {
run?: string;
};
}
Run Code Online (Sandbox Code Playgroud)
然后我想将它作为一个类实现,并在构造函数中设置属性:
class Request implements SMJSPacket {
constructor(data: any, method: string) {
this.header = {
type: 'request',
method: method || 'calld',
tag: Request.getTag()
}
this.request = data;
}
static getTag(): string {
return '_' + goog.now() + '_' + utils.getRandomBetween(1, 1000);
}
}
Run Code Online (Sandbox Code Playgroud)
但是根据编译器Request没有实现接口.我不明白如何检查它,虽然它根据构造阶段的界面填充了一切,如果用JavaScript编写,这将工作正常,类型检查Closure工具中的相同的东西也完美.我的想法是,我想将接口实现为类,因此我可以在原型中使用实用程序方法,但仍然可以轻松地转换为JSON字符串.
有任何想法吗?
谢谢
语言服务将静态分析您的界面声明,并且因为您已经表示它需要您的header成员,它应该成为类声明的一部分:
class Request implements SMJSPacket {
header: { tag: string; method: string; type: string; };
constructor(data: any, method: string) {
this.header = {
type: "request",
method: (method || "calld"),
tag: Request.getTag()
};
}
static getTag(): string {
return "tag stuff";
}
}
Run Code Online (Sandbox Code Playgroud)
别担心,输出javascript更精简:
var Request = (function () {
function Request(data, method) {
this.header = {
type: "request",
method: (method || "calld"),
tag: Request.getTag()
};
}
Request.getTag = function getTag() {
return "tag stuff";
}
return Request;
})();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14994 次 |
| 最近记录: |