[英]Typescript definition file with submodules
我在TypeScript源代码中使用Vortex Web JavaScript库,该库提供dds
对象。 这是一个片段:
var runtime = new dds.runtime.Runtime();
runtime.connect("ws://localhost:9000", "user:pass");
var chatTopic = new dds.Topic(0, 'ChatMessage');
runtime.registerTopic(chatTopic);
对于这个库,我想编写一个定义文件。
这是我目前的尝试:
declare module VortexWebClient {
interface TopicQos {
new (): TopicQos;
}
interface Topic {
new (domainID: number, topicName: string): Topic;
new (domainID: number, topicName: string, tqos: TopicQos): Topic;
new (domainID: number, topicName: string, tqos: TopicQos, topicType: string): Topic;
new (domainID: number, topicName: string, tqos: TopicQos, topicType: string, typeName: string): Topic;
}
interface Runtime {
new (): Runtime;
connect(server: string, authToken: string);
disconnect();
registerTopic(topic: Topic);
}
interface runtime {
Runtime: Runtime;
}
export interface DDS {
runtime : runtime;
Topic : Topic;
VERSION: string;
}
}
declare var dds: VortexWebClient.DDS;
这有效,但在我看来,应该使用export
关键字有更好的方法。 尤其应该避免在底部列出DDS
接口的所有成员(还有很多东西要写)。
我尝试了许多不同的方法,其中一种是以下方法。 应该避免显式创建仅作为包装器的dds接口,这样说:
declare module DDS {
export interface TopicQos {
new (): TopicQos;
}
export interface Topic {
new (domainID: number, topicName: string): Topic;
new (domainID: number, topicName: string, tqos: TopicQos): Topic;
new (domainID: number, topicName: string, tqos: TopicQos, topicType: string): Topic;
new (domainID: number, topicName: string, tqos: TopicQos, topicType: string, typeName: string): Topic;
}
interface Runtime {
new (): Runtime;
connect(server: string, authToken: string);
disconnect();
registerTopic(topic: Topic);
}
export interface runtime {
Runtime: Runtime;
}
export var VERSION: string;
}
//Here IntelliJ complaints: "Cannot find name: DDS"
declare var dds: DDS;
创建包含多个子模块的定义文件的正确方法是什么?
此行中的错误:
declare var dds: DDS;
是因为DDS
不是类型,而是模块。 但是您正在尝试将其用作类型。
相反,您可以将DDS
重命名为dds
,然后它实际上是一个变量,用于保存具有声明的内部结构的对象。
从示例开始, dds
是一个全局对象。
declare var dds: any;
它具有runtime
属性。
declare var dds: { runtime : any};
runtime
具有名为Runtime
的属性,该属性是带有不带参数的构造函数的类。
declare var dds: {
runtime : {
Runtime: typeof Runtime
}
};
declare class Runtime {
constructor();
}
Runtime
类具有一个名为connect
的方法,该方法采用两个字符串并返回void。
declare var dds: {
runtime : {
Runtime: typeof Runtime
}
};
declare class Runtime {
constructor();
connect(server: string, authToken: string): void;
}
这可以满足您的初始要求。 现在,通过将所有类型(全局的dds
除外)放入VortexWeb
模块来整理事情。
declare module VortexWeb {
export class Runtime {
constructor();
connect(server: string, authToken: string): void;
}
}
declare var dds: {
runtime : {
Runtime: typeof VortexWeb.Runtime
}
};
这就是您的入门定义。 希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.