我在我的涡旋网源代码中使用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;创建包含多个子模块的定义文件的正确方法是什么?
发布于 2015-05-19 19:10:24
这一行中的错误:
declare var dds: DDS;因为DDS不是一种类型,而是一种模块。但你想用它作为一种类型。
您可以将DDS重命名为dds,然后它实际上是一个变量,它保存了一个具有您声明的内部结构的对象。
发布于 2015-05-19 19:31:58
从示例的开头开始,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
}
};这是你的起始定义。希望这能帮上忙!
https://stackoverflow.com/questions/30326352
复制相似问题