Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RangeError: Maximum call stack size exceeded with union type #23777

Closed
cyrilletuzi opened this issue Apr 30, 2018 · 5 comments · Fixed by #23794
Closed

RangeError: Maximum call stack size exceeded with union type #23777

cyrilletuzi opened this issue Apr 30, 2018 · 5 comments · Fixed by #23794
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@cyrilletuzi
Copy link

TypeScript Version: 2.7.2 and 2.9.0-dev.20180430

Search Terms:
RangeError: Maximum call stack size exceeded

Code

  • git clone https://github.com/cyrilletuzi/angular-async-local-storage.git
  • cd angular-async-local-storage
  • git checkout newschema
  • npm test > OK
  • Uncomment test in src/lib/src/service/validation/json-validation.spec.ts lines 570-576
  • npm test

Actual behavior:
RangeError: Maximum call stack size exceeded

Expected behavior:
Should not crash.

Context:
Trying to do an union type for JSONSchema.

When giving a wrong type value with a primitive, it's OK (error reported in VS Code but tsc not crashing):

const test = jsonValidator.validate('test', { type: 1 });

But when giving a wrong type value with an array or object, no error is shown in VS Code (as if typings were valid) and tsc crashes with the error above:

const test = jsonValidator.validate('test', { type: ['string', 'number'] });

What is strange is when doing the same in a more simpler file the typing error is reported.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 30, 2018

Looks like the types are deep enough to run out of stack. on my box, raising the stack limit from default (984) to 1001 gets the types to check.

We can look and see if there are unnecessary calls that we can remove and limit the factor we add to the stack size. but ultimately, in a structural type system, eventually there is a limit to hit.

@mhegazy mhegazy added Bug A bug in TypeScript Needs Investigation This issue needs a team member to investigate its status. labels Apr 30, 2018
@mhegazy mhegazy added this to the TypeScript 3.0 milestone Apr 30, 2018
@cyrilletuzi
Copy link
Author

What I try to achieve is a simple exclusive union type: it's just a few interfaces with different type values. Is there a particular reason it reaches the stack limit? Something I can simplify?

@ahejlsberg
Copy link
Member

ahejlsberg commented Apr 30, 2018

@sandersn @mhegazy The culprit here appears to be some logic in getApparentTypeOfContextualType that tries to narrow the contextual type based on a discriminant in the source type. The way that code is written we reach back down into the type for which we just reached up to the parent and we go into what appears to be an infinite loop (or something that is very, very deep). The logic that attempts to narrow based on a discriminant needs to be more selective (or should I say discriminating) about when calls checkExpression as it is reaching down. Perhaps we should only do it when the expression is a literal. Certainly we shouldn't when the expression is an array literal as that could never be a discriminant.

@cyrilletuzi
Copy link
Author

@ahejlsberg An array or an object (same issue with an object as type).

@ahejlsberg
Copy link
Member

@cyrilletuzi I'm not sure I understand what you mean.

@ahejlsberg ahejlsberg assigned ahejlsberg and unassigned sandersn Apr 30, 2018
@ahejlsberg ahejlsberg removed the Needs Investigation This issue needs a team member to investigate its status. label Apr 30, 2018
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Apr 30, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants