Typescript: RangeError when trying to infer type for Array.prototype.flat()

Created on 2 May 2020  ·  4Comments  ·  Source: microsoft/TypeScript

TypeScript Version: 3.9.1-rc, 4.0.0-dev.20200430

Search Terms: Array flat RangeError Maximum call stack size exceeded

Code

class Foo <T> {
  arr: T[] = [];

  bar (
    depth = 1,
  ) {
    return this.arr.flat(depth);
  }
}

Expected behavior:
Code compiles without errors.

Actual behavior:
TSC crashes with RangeError: Maximum call stack size exceeded. Edit: Only happens when either --watch or --declaration flag is set.

Playground Link: Couldn't figure out how to add newer libs for Array, and not super useful anyway, as the compiler crashes

Related Issues:
https://github.com/microsoft/TypeScript/pull/32131 seems to be the breaking PR
https://github.com/microsoft/TypeScript/issues/36554 seems related maybe

edit: simplified code example

Bug

Most helpful comment

I can also produce it here

function foo<T>(arr: T[], depth: number) {
  return arr.flat(depth);
}

And this compiles fine

function foo<T>(arr: T[]) {
  return arr.flat(5);
}

But this does not

function foo<T>(arr: T[]) {
  return arr.flat(<number> 5);
}

And this is the tsc (4.0.0-dev.20200501) command:

tsc flat-test.ts --lib es2019 -m ESNext -t ESNEXT -w

Edit: After corresponding with @0kku he discovered it also happens with --declaration

tsc flat-test.ts --lib es2019 -m ESNext -t ESNEXT --declaration

All 4 comments

I can also produce it here

function foo<T>(arr: T[], depth: number) {
  return arr.flat(depth);
}

And this compiles fine

function foo<T>(arr: T[]) {
  return arr.flat(5);
}

But this does not

function foo<T>(arr: T[]) {
  return arr.flat(<number> 5);
}

And this is the tsc (4.0.0-dev.20200501) command:

tsc flat-test.ts --lib es2019 -m ESNext -t ESNEXT -w

Edit: After corresponding with @0kku he discovered it also happens with --declaration

tsc flat-test.ts --lib es2019 -m ESNext -t ESNEXT --declaration

Not able to reproduce the error on 3.9.1-rc or 4.0.0-dev.20200430. Compiles.

@pirix-gh It doesn't happen in normal mode, it happens in watch mode.

Looks like it doesn't happen if I comment out "declaration": true from my tsconfig. Also with -w flag it happens regardless of that config option. Here's my tsconfig:

{
  "compilerOptions": {
    "lib": ["es2019"],
    "declaration": true,
    "outDir": "./dist",
    "rootDir": "./src",
  }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

wmaurer picture wmaurer  ·  3Comments

DanielRosenwasser picture DanielRosenwasser  ·  3Comments

weswigham picture weswigham  ·  3Comments

dlaberge picture dlaberge  ·  3Comments

fwanicka picture fwanicka  ·  3Comments