Skip to content

RangeError when trying to infer type for Array.prototype.flat() #38298

Closed
@0kku

Description

@0kku

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:
#32131 seems to be the breaking PR
#36554 seems related maybe

edit: simplified code example

Activity

b-fuze

b-fuze commented on May 2, 2020

@b-fuze

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
millsp

millsp commented on May 2, 2020

@millsp
Contributor

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

b-fuze

b-fuze commented on May 2, 2020

@b-fuze

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

0kku

0kku commented on May 2, 2020

@0kku
Author

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",
  }
}
added this to the TypeScript 4.0 milestone on Jun 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

    Participants

    @weswigham@RyanCavanaugh@b-fuze@millsp@0kku

    Issue actions

      RangeError when trying to infer type for Array.prototype.flat() · Issue #38298 · microsoft/TypeScript