2

Using typescript@3.6 and given this tsconfig.json

{
  "compilerOptions": {
    "target": "ES2018",
    "lib": [
      "ES2019",
      "DOM"
    ],
    "module": "commonjs",
    "pretty": true,
    "outDir": "dist",
    "sourceMap": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "strictNullChecks": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": [
    "src/**/*.ts",
    "tests/**/*.ts",
    "cli.ts"
  ]
}

I assumed that I could make use of the new es2019 features while still generating valid es2018 code. So I thought I could use Array.prototype.flat and Array.prototype.flatMap.

Yet when compiling and running my code I get the error:

TypeError: anArray.map(...).flat is not a function

I am using node@10 for now and it does not natively support these functions, node@11 would but I don't want to upgrade node yet.

I was expecting to make use of future features through typescript. Yet when I look in the compiled js-file, I see the native construct being used -- so I am not wondering why it fails. I was expecting a polyfill there. I am wondering how to get es2019 features while still targeting older versions of ecma-script.

Am I enabling it wrong? What am I missing?

(This question is not about finding alternatives for the prototype functions. I am used of using lodash, that would be my fallback.)

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
  • 1
    `lib` is you informing the compiler about what libraries will be available in the runtime environment; it does not add any functionality to the runtime environment. – jcalz Sep 12 '19 at 17:51
  • See e.g. https://stackoverflow.com/questions/52283892/transpiling-array-prototype-flat-away-with-babel – jonrsharpe Sep 12 '19 at 17:51
  • 1
    @jcalz You can add that as an answer. My assumption were wrong. – k0pernikus Sep 12 '19 at 17:52

0 Answers0