HiveBrain v1.2.0
Get Started
← Back to all entries
patterntypescriptangularCritical

flatMap, flat, flatten doesn't exist on type any[]

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
existflattenanyflatdoesnflatmaptype

Problem

I'm using Chrome 70 and Chrome does add methods .flatMap, .flatten, .flat. So my code does run as expected. Unfortunately, TypeScript doesn't like it.

// data.flatMap lint error
export const transformData = (data: any[]) => data.flatMap(abc => [
   parentObj(abc),
   ...generateTasks(abc)
]);


The warning I got is TS2339: Property 'flatMap' does not exist on type 'any[]'.

I'm using Angular 6, which uses Typescript ~2.9.2, and I already include import 'core-js/es7/array'; in polyfills.ts.

My guess is that there is no typing for these methods, and I did try to npm run -dev @types/array.prototype.flatmap but still not solve.

Solution

You should add es2019 or es2019.array to your --lib setting for TypeScript to recognize array.flat() and flatMap().

Example:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "es2019"
    ]
  }
}


Previously this was available as part of esnext or esnext.array, but it's now officially part of ES2019.

Code Snippets

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "es2019"
    ]
  }
}

Context

Stack Overflow Q#53556409, score: 570

Revisions (0)

No revisions yet.