Typescript replaceAll not transpiled in es5

700 Views Asked by At

I am using Typescript with ESNEXT (as of 2021) replaceAll in my code, because it is clearer than using a regexp.

I am transpiling to es5, but in the dist folder, in the traspiled js code, replaceAll remains as it is and there is a TypeError: replaceAll is not a function error.

I thought that Typescript would have traspiled it to es5, in a way that it works with es5. Why does it not do it? How can I fix this problem?

My TS config is:

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

There are 1 best solutions below

1
On BEST ANSWER

TypeScript will transpile modern JS syntax to ES5 but not do the polyfilling (runtime apis).

You can use core-js for that. Add the following to your application entry point:

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

More

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