定位到es6或更高版本时,无法将外部模块编译为amd或commonjs

test.ts

export class Test { whatever(): Promise<any> { return undefined; } } 

试图用旧版本编译:

 $ tsc --version message TS6029: Version 1.4.1.0 $ tsc --target es6 --module commonjs test.ts $ cat test.js var Test = (function () { function Test() { } Test.prototype.whatever = function () { return undefined; }; return Test; })(); exports.Test = Test; 

这可以。 现在用新版本:

 $ ./node_modules/.bin/tsc --version message TS6029: Version 1.5.0-beta $ ./node_modules/.bin/tsc --target es6 --module commonjs test.ts error TS1204: Cannot compile external modules into amd or commonjs when targeting es6 or higher. 

这是为什么? 我正在开发NodeJS应用程序,所以我必须使用commonjs。 另外,我需要本地的承诺,因此es6的目标。

 $ ./node_modules/.bin/tsc --target es5 --module commonjs test.ts test.ts(2,14): error TS2304: Cannot find name 'Promise'. 

如果您正在编译支持ES6的目标,则应该使用ES6模块导入,而不是commonjs或AMD。

 import * as Promise from 'Promise'; 

如果您提供--target ES6则在编译时删除--module标志。