我目前正在尝试为OpenLayers构建一个TypeScript定义文件。
问题在于,在OpenLayers中,某些类可以转换为TypeScript中的模块和类。
例如,模块Protocol
中有OpenLayers
个类,模块Response
中有一个类OpenLayers.Protocol
。
我如何在TypeScript中对其进行建模?我可以将Protocol
作为一个类并将Response
类定义为导出的内部类吗?你会如何解决这个问题?
答案 0 :(得分:1)
使用构造函数类型将Response
声明为static
Protocol
字段,并返回定义interface
类的Response
:
declare module OpenLayers {
export interface IProtocolResponse {
foo(): void;
}
export class Protocol {
static Response: new () => IProtocolResponse;
}
}
var response = new OpenLayers.Protocol.Response();
response.foo();
修改强>
或者正如Anders在this discussion list question中指出的那样,你可以用这种方式为内部类创建多个构造函数:
declare module OpenLayers {
export interface IProtocolResponse {
foo(): void;
}
export class Protocol {
static Response: {
new (): IProtocolResponse;
new (string): IProtocolResponse;
};
}
}
var response = new OpenLayers.Protocol.Response('bar');
response.foo();
这两种方法的主要缺点是你无法从OpenLayers.Protocol.Response
派生一个班级。
答案 1 :(得分:0)
这是我更新的答案,我希望有所帮助 - 它应该让您开始定义OpenType:
declare module OpenType {
export class Protocol {
constructor();
Request;
}
}
var x = new OpenType.Protocol();
var y = new x.Request();
var z = x.Request;