簡體   English   中英

繼承帶有導出的類和模塊的TypeScript

[英]Inheritance TypeScript with exported class and modules

我正在使用typescript繼承瘋狂。 我不知道為什么,但它無法解決我想繼承的類。

LIB /班/ Message.class。 TS

///<reference path='./def/lib.d.ts'/>
///<reference path='./def/node.d.ts'/>

export module SharedCommunication {
    export class Message{
         // Stuff
    }
}

LIB /班/ ValidatorMessage.class。 TS

///<reference path='./def/lib.d.ts'/>
///<reference path='./def/node.d.ts'/>
///<reference path='Message.class.ts'/>

export module SharedCommunication { 
    export class ValidatorMessage extends Message{
        private  _errors;
    }    
}

消息無法解決。 我也嘗試過SharedCommunication.Message,但它是一樣的。 我引用了課程,所以我根本不明白發生了什么。 你有什么主意嗎?

我嘗試沒有模塊(兩個類沒有在任何模塊中),但它是相同的。 我需要導出類(以及模塊,如果我使用它)從另一個node_module:typescript.api中獲取它們,我用它來加載類並在節點中使用它。

LIB / message.js

var Message = require('./classes/Message.class.ts');

module.exports = Message.SharedCommunication.Message;

這里的訣竅是什么? 因為我在使用繼承的不同文件夾中的同一項目上有源代碼,沒有模塊或導出。 謝謝。

ValidatorMessage.class.ts應如下所示:

///<reference path='./def/lib.d.ts'/>
///<reference path='./def/node.d.ts'/>

import message = require('./Message.class');

export module SharedCommunication { 
    export class ValidatorMessage extends message.SharedCommunication.Message {
        private  _errors;
    }
}

在文件的頂層有一個export module通常是多余的,因為文件本身無論如何都構成了一個命名空間。

比爾在你的另一個問題中提到了這一點,但是如果你剛剛開始使用TypeScript,我會再次提醒使用RequireTS - 這聽起來很不成熟,很可能引起很多混亂。

從mododule聲明中取出導出:

module SharedCommunication 
{ 
    export class ValidatorMessage extends message.SharedCommunication.Message 
    {
        private  _errors;
    }
}

暫無
暫無

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

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