Reputation: 6866
I'm using the Vortex Web JavaScript library within my TypeScript source code which provides the dds
object. Here's a snippet:
var runtime = new dds.runtime.Runtime();
runtime.connect("ws://localhost:9000", "user:pass");
var chatTopic = new dds.Topic(0, 'ChatMessage');
runtime.registerTopic(chatTopic);
For this library I want to write a definition file.
Here's my current attempt:
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;
This works but it looks to me that there should be a better way using the export
keyword. Especially listing all the members for the DDS
interface at the bottom (there are still a lot more to write) should be avoided.
I tried many different ways, one of them was the following. It should avoid explicitly creating the dds interface which is just a wrapper so to say:
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;
What's the correct way of creating a definition file which includes several submodules?
Upvotes: 0
Views: 466
Reputation: 4647
Starting from the beginning of your example, dds
is a global object.
declare var dds: any;
It has a runtime
property.
declare var dds: { runtime : any};
runtime
has a property called Runtime
that is a class with a constructor that takes no arguments.
declare var dds: {
runtime : {
Runtime: typeof Runtime
}
};
declare class Runtime {
constructor();
}
The Runtime
class has a method called connect
that takes two strings and returns void.
declare var dds: {
runtime : {
Runtime: typeof Runtime
}
};
declare class Runtime {
constructor();
connect(server: string, authToken: string): void;
}
This fills your initial requirement. Now let's tidy things up by putting all of the types (except dds
which is global) in a VortexWeb
module.
declare module VortexWeb {
export class Runtime {
constructor();
connect(server: string, authToken: string): void;
}
}
declare var dds: {
runtime : {
Runtime: typeof VortexWeb.Runtime
}
};
That's your starter definition. Hope that helps!
Upvotes: 1
Reputation: 116714
The error in this line:
declare var dds: DDS;
Is because DDS
is not a type, it's a module. But you're trying to use it as a type.
You could instead just rename DDS
to dds
, and it would then effectively be a variable holding an object with the internal structure you've declared.
Upvotes: 1