2014-05-13 136 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