【问题标题】:How to get support of generators in typescript without setting target to ES6?如何在不将目标设置为 ES6 的情况下在 typescript 中获得对生成器的支持?
【发布时间】:2016-01-11 03:55:16
【问题描述】:

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

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

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

"module":"commonjs 不允许与 "target":"ES6" 一起转换选项。

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

【问题讨论】:

  • 问题的另一方面是 TypeScript 自己将 var fs = require('fs'); 转换为 import * as fs from 'fs';。所以,没有办法避免import 命令。

标签: node.js typescript


【解决方案1】:

正如您在TypeScript roadmap(1.7 版)中看到的,当前问题之一是"Support --module with --target es6"

恐怕在 TypeScript 1.7 发布之前,您需要一个临时解决方案。也许是Polyfill for the ES6 Module LoaderSystemJS

【讨论】:

    【解决方案2】:

    这些设置对我有用:

    tsconfig.json

    {
      "compilerOptions": {
        "target":"ES6",
        "moduleResolution": "classic",
      }
    }
    
    • ES6 支持生成器
    • 没有import 东西转译由于 "moduleResolution": "classic"

    所以问题就解决了!

    【讨论】:

    • 我们仍然无法仅使用 ts 来实现这一点,我想我们需要像 babel 这样的单独编译器来将最终的 es6 文件转换为 es5
    • OP: "...没有将目标设置为 ES6?"还有OP:"target":"ES6"
    【解决方案3】:

    另一种获得我想要的一切的方法是构建堆栈:

    1. 将 TS 转译为 ES6
    2. 使用 Babel 将 es6-js 转换为 es5-js

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-31
      • 2014-02-22
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-21
      相关资源
      最近更新 更多