0

我在我的代码中使用带有ESNEXT(截至 2021 年)的 Typescript replaceAll,因为它比使用正则表达式更清晰。

我正在编译es5,但在dist文件夹中,在经过转换的 js 代码中,replaceAll保持原样并且出现TypeError: replaceAll is not a function错误。

我认为 Typescript 会以与 es5 一起使用的方式将其转换为 es5。为什么不这样做?我该如何解决这个问题?

我的 TS 配置是:

{
    "compilerOptions": {
        "strict": true,
        "target": "es5",
        "lib": [
            "ES2021"
        ],
        "sourceMap": true,
        "outDir": "../dist/server",
        "baseUrl": ".",
        "resolveJsonModule": true,
        "downlevelIteration": true,
        "paths": {
            "@/*": [
                "./*"
            ]
        }
    }
}
4

1 回答 1

1

TypeScript 会将现代 JS语法转换为 ES5,但不会进行 polyfill(运行时 API)。

您可以为此使用 core-js。将以下内容添加到您的应用程序入口点:

import 'core-js'; // <- at the top of your entry point

更多的

https://www.npmjs.com/package/core-js

于 2021-09-09T21:44:26.433 回答