TypeScript – 使用<reference path =“…”>标记

我想添加一个插件到TypeScript编译器,所以我添加了我的代码,我编译了tsc.ts. 它编译正确,但是当我运行时,它缺less一些在io.ts中声明的variables。 我删除了我的更改,但仍然无效。 所以我试过这个:

文件:a.ts

var a : number = 5; 

文件:b.ts

 ///<reference path='a.ts' /> console.log(a); 

然后编译: tsc b.ts给我a.js和b.js. 当我尝试运行b.js(我做node b.js )时,variablesa是未定义的。 这是b.js的内容:

 ///<reference path='a.ts' /> console.log(a); 

所以a是不确定的,因为a在这个文件中是不存在的,所以a.ts

我编译错了,还是执行错了..或者是什么?

如果您在节点下运行,则应该使用exportimport来pipe理跨文件依赖关系。

a.ts

 export var x = 5; 

b.ts

 import a = require('./a'); console.log(ax); 

--module commonjs编译

如果你在网上运行,你可以用--out连接成一个文件,但是在节点下通常不是一个好的select。