繁体   English   中英

带有子模块的打字稿定义文件

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM