【问题标题】:Unable to add ES2021 in compiler target - Typescript无法在编译器目标中添加 ES2021 - Typescript
【发布时间】:2022-08-23 16:56:25
【问题描述】:

我有以下tsconfig.json,我无法将 ES2021 版本用于编译器目标。

{
  \"compilerOptions\": {
    \"target\": \"es2016\",
    \"module\": \"commonjs\",
    \"outDir\": \"./dist\",
    \"strict\": true,
    \"esModuleInterop\": true,
    \"inlineSourceMap\": true,
    \"moduleResolution\": \"node\"
  }
}

每当我使用 ES2021 时,都会出现错误:

tsconfig.json:3:15 - 错误 TS6046:\'--target\' 选项的参数必须是:\'es3\'、\'es5\'、\'es6\'、\'es2015\'、\' es2016\'、\'es2017\'、\'es2018\'、\'es2019\'、\'es2020\'、\'esnext\'。

我尝试添加如下:

{
  \"compilerOptions\": {
    \"target\": \"ES2021\",
    \"module\": \"commonjs\",
    \"outDir\": \"./dist\",
    \"strict\": true,
    \"esModuleInterop\": true,
    \"inlineSourceMap\": true,
    \"moduleResolution\": \"node\",
    \"lib\": [ \"ES2021\" ]
  }
}

但仍然没有运气。我最初尝试使用es2021,然后在浏览了一些网站后移至ES2021

并经历了几条链herehere,其中一个问题仍然存在,其他建议使用 4.3.5 以上的 TSC 版本来解决此问题。而我的站在 4.6.2

我需要来自 ES2021 的 replaceAll 逻辑。这就是我寻找这个的原因。我在这里错过了什么吗?我们有解决办法吗?

版本

  • VS 代码 - 1.65.2
  • Node.js - 14.16.0
  • 打字稿 - 4.6.2

错误图片

错误跟踪

tsconfig.json:3:15 - error TS6046: Argument for \'--target\' option must be: \'es3\', \'es5\', \'es6\', \'es2015\', \'es2016\', \'es2017\', \'es2018\', \'es2019\', \'es2020\', \'esnext\'.

3     \"target\": \"ES2021\",
                ~~~~~~~~

tsconfig.json:10:14 - error TS6046: Argument for \'--lib\' option must be: \'es5\', \'es6\', \'es2015\', \'es7\', \'es2016\', \'es2017\', \'es2018\', \'es2019\', \'es2020\', \'esnext\', \'dom\', \'dom.iterable\', \'webworker\', \'webworker.importscripts\', \'scripthost\', \'es2015.core\', \'es2015.collection\', \'es2015.generator\', \'es2015.iterable\', \'es2015.promise\', \'es2015.proxy\', \'es2015.reflect\', \'es2015.symbol\', \'es2015.symbol.wellknown\', \'es2016.array.include\', \'es2017.object\', \'es2017.sharedmemory\', \'es2017.string\', \'es2017.intl\', \'es2017.typedarrays\', \'es2018.asyncgenerator\', \'es2018.asynciterable\', \'es2018.intl\', \'es2018.promise\', \'es2018.regexp\', \'es2019.array\', \'es2019.object\', \'es2019.string\', \'es2019.symbol\', \'es2020.bigint\', \'es2020.promise\', \'es2020.string\', \'es2020.symbol.wellknown\', \'esnext.array\', \'esnext.symbol\', \'esnext.asynciterable\', \'esnext.intl\', \'esnext.bigint\', \'esnext.string\', \'esnext.promise\'.

10     \"lib\": [ \"ES2021\" ]
                ~~~~~~~~


Found 2 errors.
  • 如果您将 TypeScript 版本更新到最新版本会怎样?在使用 v4.6+ 的操场上,有一个 ES2021 选项
  • 让我检查一下凯利斯。
  • 没有工作凯利。我在问题中更新了图像和跟踪。
  • 您正在运行什么命令来打印这些错误?
  • 我正在使用 VS Code 中的 \'Run->Start Debugging\',这就是我收到该错误的时候。

标签: typescript


【解决方案1】:

我想这个答案来晚了,但对于任何过来的人来说:

原因是 Node 14.16.0 不支持 ES2021。正如您在此处看到的 https://node.green/#ES2021 ,ES2021 支持从 Node 15.14 开始。如果你想使用 ES2021,你将需要使用 Node 15(或者实际上是 Node 16,由于 Node 版本的工作方式)或更高版本。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    • 2020-07-31
    • 2011-07-16
    • 2017-03-11
    相关资源
    最近更新 更多