如何在不将目标设置为 ES6 的情况下获得打字稿中生成器的支持? [英] How to get support of generators in typescript without setting target to ES6?

查看:33
本文介绍了如何在不将目标设置为 ES6 的情况下获得打字稿中生成器的支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有情况.我使用带有 --harmony 标志的 nodejs 来获得对生成器的支持.接下来我试图将我的项目切换到 TypeScript 并遇到问题:在 "target":"ES6" 模式下,它按原样转换 import 命令(而不是 要求).

Got a situation here. I use nodejs with --harmony flag to get support of generators. Next i'm trying to switch my project to TypeScript and get a problem: in "target":"ES6" mode it transpiles import commands as is (instead of require).

带有 --harmony 标志的节点不支持:

And node with --harmony flag doesn't support that:

import * as fs from 'fs';
^^^^^^
SyntaxError: Unexpected reserved word

转译选项 "module":"commonjs 不允许与 "target":"ES6" 一起使用.

Transpiling option "module":"commonjs isn't allowed with "target":"ES6".

有没有人在不使用任何外部 require/import 实用程序的情况下解决了这个问题?

Have anyone solved this problem without using any external require/import utilities?

推荐答案

这些设置对我有用:

tsconfig.json

tsconfig.json

{
  "compilerOptions": {
    "target":"ES6",
    "moduleResolution": "classic",
  }
}

  • ES6 对生成器的支持
  • 没有 import 内容转译,因为"moduleResolution": "经典"
    • ES6 support for generators
    • No import stuff transpiling due to "moduleResolution": "classic"
    • 这样问题就解决了!

      这篇关于如何在不将目标设置为 ES6 的情况下获得打字稿中生成器的支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆