2014-05-13 105 views
2

我試圖內的NodeJS使用Dictionary類從typescript-collections的NodeJS,打字稿及打字稿的集合

/// <reference path="../../../scripts/collections.ts" /> 
var collections = require('../../../scripts/collections'); 

export class AsyncProcessorCommand implements interfaces.IAsyncProcessorCommand 
{ 
    public static Type = 'RabbitMon.AsyncProcessorCommand:RabbitMon'; 
    public GetType =() => 'RabbitMon.AsyncProcessorCommand:RabbitMon'; 

    public ID: string; 

    constructor(public Action: string, public Arguments?: collections.Dictionary<string, string>) { 
     this.ID = uuid.v4(); 

     this.Arguments = new collections.Dictionary<string, string>(); 
     //I've also tried the following 
     //this.Arguments = new collections.Dictionary<string, string>((key: string) => sha1(key)); 
    } 
} 

但我不斷收到對new Dictionary以下錯誤:

TypeError: undefined is not a function 

任何人都知道這裏發生了什麼?我也非常樂意用一個更好的TS收藏庫替代...

回答

0

你有一個internal vs external modules問題。

打字稿收藏庫被寫成內部模塊 - 一個標準的JavaScript文件,你可以隨便扔在到網頁中的script標籤。

節點的require,然而,期待將分配東西exports,換句話說,是CommonJS的兼容文件的外部模塊。發生了什麼事是node.js找到collections.js,執行它,然後返回exports對象評估文件。因爲它只是一個普通的JS文件,導出的對象是{} - 爲空。

最好的解決將是:

  1. 替換您參考collections.ts一到collections.d.ts,只是爲了正確性的緣故(運行tsc --d collection.ts生成該文件)
  2. 使用some solution裝載「香草」 JS文件在節點中。一個好的單線(從鏈接的問題)是eval(require('fs').readFileSync('./path/to/file.js', 'utf8'));
+0

它感覺有點凌亂,但是,是的,這工作。 – Michael