简体   繁体   English

编译具有所有功能的Node.js的TypeScript

[英]Compile TypeScript for Node.js with all features

If I compile TypeScript for Node.js with 如果我使用以下命令为Node.js编译TypeScript

tsc --module commonjs --target ES5

I can't use async / await or generators because tsc doesn't know how to compile it to ES5. 我不能使用async / await或生成器,因为tsc不知道如何将其编译为ES5。

That can be fixed if I compile TypeScript for Node.js with 如果我使用以下命令为Node.js编译TypeScript,可以解决此问题

tsc --module commonjs --target ES6

but then I can't use default parameters or destructuring assignment, because Node.js doesn't support them. 但是后来我不能使用默认参数或破坏结构分配,因为Node.js不支持它们。 tsc knows how to compile those to ES5, but it only does it if you actually target ES5. tsc知道如何将它们编译为ES5,但是只有在您实际针对ES5的情况下,它才会这样做。

How can I target Node.js with support for all of TypeScript's features? 如何以支持TypeScript的所有功能为目标的Node.js?

You should be fine with running your ES6 compiled code in latest node.js (v5.x). 您可以在最新的node.js(v5.x)中运行ES6编译的代码。 To enable the particular ES6 featuresthat are not enabled by default you should use --harmony flags. 要启用默认情况下未启用的特定ES6功能,应使用--harmony标志。 Like this for example: 例如:

node --harmony-destructuring 节点-和谐解构

For the complete list of possible harmony features to play with - run: 有关可使用的和声功能的完整列表,请运行:

node --v8-options | 节点--v8-options | grep harmony grep和谐

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM