Skip to content

Fix recursive mapped type infinite recursion - #21134

Merged
Nathan Shively-Sanders (sandersn) merged 5 commits into
masterfrom
fix-recursive-mapped-type-infinite-recursion
Jan 11, 2018
Merged

Fix recursive mapped type infinite recursion#21134
Nathan Shively-Sanders (sandersn) merged 5 commits into
masterfrom
fix-recursive-mapped-type-infinite-recursion

Conversation

@sandersn

@sandersn Nathan Shively-Sanders (sandersn) commented Jan 10, 2018

Copy link
Copy Markdown
Member

Fixes #17847

isGenericMappedType indirectly calls getResolvedBaseConstraint, but is called indirectly from inside getResolvedBaseConstraint. This avoids the circularity check in getResolvedBaseConstraint, which causes an infinite recursion for certain malformed types.

Interestingly, I found four approaches that fixed the bug:

  1. Delete lines 6559-6561 (if (isGenericMappedType(t)) { return emptyObjectType; })
  2. Pass typeStack to isGenericMappedType and check it in getConstraintOfTypeParameter instead of calling hasNonCircularBaseConstraint.
  3. Clone isGenericMappedType and its callees into getResolvedBaseConstraint (and check typeStack).
  4. Clone isGenericMappedType and its callees into getResolvedBaseConstraint, and merge into a single function (that checks typeStack).

And Wesley Wigham (@weswigham) suggested a fifth,

  1. Remove typeStack and use push/popTypeResolution instead.

Previously, I decided on (2) because it avoids forking the code paths, but (5) reuses code even better.

@sandersn

Copy link
Copy Markdown
Member Author

Wesley Wigham (@weswigham), I got push/popTypeResolution to work by making circularConstraintType behave the same way as undefined, since it should. I think this change is ready to go.

@sandersn
Nathan Shively-Sanders (sandersn) deleted the fix-recursive-mapped-type-infinite-recursion branch January 11, 2018 18:24
@microsoft Microsoft (microsoft) locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stack overflow with incorrect mapped type as constraint of type parameter

2 participants