簡體   English   中英

如何在打字稿聲明文件中定義單例javascript類

[英]How to define a singleton javascript class in a typescript declaration file

以下代碼位於我的“ test-file.js”文件中,我想將其移植到“ test-file.d.ts”文件中,以便隨后將其導入到我的“ test1.ts”中,並針對Typescript中的JavaScript“類型”。

我無法弄清楚在“ test-file.d.ts”中如何以Typescript存在相同的實現方式來定義以下內容。

(function(application) {

application.Message = function() {

    this.m_messageContents = "New Message";

};

application.Message.prototype.getApplicationMessageContents = function() {
    return this.m_messageContents;
};

application.SystemFactory = (function(){

var factory =
    /**
     * @lends application.SystemFactory
     */
    {
        createMessage: function() {
            return application.Message();
        }
    };
    return factory;

}());

}(application));

我不想將此文件重新編碼為* .ts文件,而是如上所述,我想將其導入到我的“ test1.ts”中。

謝謝。

讓我看看您是否可以:

declare module application {
    export class Message {
        getApplicationMessageContents(): string;
    }

    export class SystemFactory {
        static createMessage(): Message;
    }
}

您可以這樣使用:

let message = application.SystemFactory.createMessage();
let content = message.getApplicationMessageContents();

如您所見,您將聲明static createMessage(): Message; 作為靜態而非類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM