【问题标题】:ES6-shim from Angular2 conflicts with Typescript lib.es6.d.ts [duplicate]来自 Angular2 的 ES6-shim 与 Typescript lib.es6.d.ts 冲突 [重复]
【发布时间】:2016-01-09 13:33:17
【问题描述】:

我想将我用 Typescript 编写的 Angular2 应用程序编译成 ES6 代码,计划最近使用 Babel 将其编译成 ES5。但我得到了错误:

node_modules/angular2/typings/es6-shim/es6-shim.d.ts(6,14): error TS2300: Duplicate identifier 'PropertyKey'.
<...>
node_modules/typescript/lib/lib.es6.d.ts(3841,14): error TS2300: Duplicate identifier 'PropertyKey'.
<...>

我已尝试将指令 noLib 添加到我的 tsconfig.json 来解决此问题,但其中一个角度文件 (zone.d.ts) 需要导入 es6-shim.d.ts

/// <reference path="es6-shim/es6-shim.d.ts" />

它与主要的 typescript ES6 库 (lib.es6.d.ts) 冲突。

我该如何解决这个问题?

【问题讨论】:

  • 您确定要编译到 ES5 吗?当您尝试将 angular2 编译为 es6 时,通常会显示此错误,因为 TSC 将提供它自己的 es6 库。检查 tsconfig.json 中的目标。
  • @toskv 我正在尝试将项目编译为 ES6,因为我想使用只能通过 Babel 编译使用的Symbol.iterator
  • @GünterZöchbauer,所以,这是一个错误?我认为它可以通过一些设置魔法来解决,但如果它是一个错误,我似乎无法做到。感谢您的帮助。
  • 这个问题不是因为编译时引用了2个es6-shim.d.ts文件吗?听起来和这个问题一样github.com/DefinitelyTyped/DefinitelyTyped/issues/5015

标签: javascript angular typescript


【解决方案1】:

您在 tsconfig.json 中尝试过吗?:

{
  "compilerOptions": {
    "target": "ES6"
  }
}

顺便说一句,如果您已经在使用 typescript,为什么还要使用 babel?它们都是编译器,您只需要一个。如果你的代码是用 typescript 写的,typescript 编译器会把它编译成 js,你不能重新编译它,因为它已经编译过了。只需选择目标编译器选项,无论您将使用 es5 还是 es6 语法,如果您使用 es6 语法编译它,请确保在您的应用程序中添加一个 es6 shim,以便它在尚不识别 es6 的浏览器上运行。

【讨论】:

  • 是的,我试过了。而且,这是问题的根源,因为es6-shimlib.es6.d.ts 冲突,lib.es6.d.ts 是在设置--target es6 时使用的。关于 Babel - 我使用它是因为 typescript 本身不能像 babel 那样编译 es6 生成器和迭代器。
  • 有没有办法解决这个问题并且仍然以 es5 为目标?我需要将代码输出到 5 而不是 6。但这确实使错误消失了。那么在你的应用中加入 es6 shim 会让一切正常吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-18
  • 1970-01-01
相关资源
最近更新 更多