首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有子模块的类型记录定义文件

带有子模块的类型记录定义文件
EN

Stack Overflow用户
提问于 2015-05-19 12:59:30
回答 2查看 439关注 0票数 0

我在我的涡旋网源代码中使用JavaScript库,它提供了dds对象。这里有一个片段:

代码语言:javascript
复制
var runtime = new dds.runtime.Runtime();
runtime.connect("ws://localhost:9000", "user:pass");

var chatTopic = new dds.Topic(0, 'ChatMessage');
runtime.registerTopic(chatTopic);

对于这个库,我想编写一个定义文件。

以下是我目前的尝试:

代码语言:javascript
复制
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接口,因为它只是一个包装器,因此可以这样说:

代码语言:javascript
复制
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;

创建包含多个子模块的定义文件的正确方法是什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-19 19:10:24

这一行中的错误:

代码语言:javascript
复制
declare var dds: DDS;

因为DDS不是一种类型,而是一种模块。但你想用它作为一种类型。

您可以将DDS重命名为dds,然后它实际上是一个变量,它保存了一个具有您声明的内部结构的对象。

票数 1
EN

Stack Overflow用户

发布于 2015-05-19 19:31:58

从示例的开头开始,dds是一个全局对象。

代码语言:javascript
复制
declare var dds: any;

它有一个runtime属性。

代码语言:javascript
复制
declare var dds: { runtime : any};

runtime有一个名为Runtime的属性,它是一个类,具有不带参数的构造函数。

代码语言:javascript
复制
declare var dds: {
  runtime : {
    Runtime: typeof Runtime
  }
};

declare class Runtime {
  constructor();
}

Runtime类有一个名为connect的方法,它接受两个字符串并返回void。

代码语言:javascript
复制
declare var dds: {
  runtime : {
    Runtime: typeof Runtime
  }
};

declare class Runtime {
  constructor();
  connect(server: string, authToken: string): void;
}

这将满足您的初始需求。现在,让我们将所有类型(除了dds是全局的)放在一个VortexWeb模块中来整理。

代码语言:javascript
复制
declare module VortexWeb {
  export class Runtime {
    constructor();
    connect(server: string, authToken: string): void;
  }
}

declare var dds: {
  runtime : {
    Runtime: typeof VortexWeb.Runtime
  }
};

这是你的起始定义。希望这能帮上忙!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30326352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档