Skip to content

Stack overflow with incorrect mapped type as constraint of type parameter #17847

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

Closed
sandersn opened this issue Aug 16, 2017 · 6 comments · Fixed by #21134
Closed

Stack overflow with incorrect mapped type as constraint of type parameter #17847

sandersn opened this issue Aug 16, 2017 · 6 comments · Fixed by #21134
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@sandersn
Copy link
Member

function sum<T extends { [P in T]: number }, K extends keyof T>() {
    n += v[k];
}

This incorrect code puts the compiler in an infinite loop:

     RangeError: Maximum call stack size exceeded
      at Object.contains (src/compiler/core.ts:276:5)
      at getBaseConstraint (src/compiler/checker.ts:5955:21)
      at getResolvedBaseConstraint (src/compiler/checker.ts:5949:36)
      at hasNonCircularBaseConstraint (src/compiler/checker.ts:5936:20)
      at getConstraintOfTypeParameter (src/compiler/checker.ts:5906:20)
      at getConstraintTypeFromMappedType (src/compiler/checker.ts:5761:56)
      at isGenericMappedType (src/compiler/checker.ts:5798:84)
      at computeBaseConstraint (src/compiler/checker.ts:5997:21)
      at getBaseConstraint (src/compiler/checker.ts:5960:32)
      at computeBaseConstraint (src/compiler/checker.ts:5969:38)
      at getBaseConstraint (src/compiler/checker.ts:5960:32)
@mhegazy mhegazy added the Bug A bug in TypeScript label Aug 16, 2017
@arciisine
Copy link

I'm having a similar issue to this, but can't reproduce the issue in a minimal case. In the minimal case it works properly, but inside of my project, I have this same stack trace, but my code is:

export type MatchQuery<T> = {
  [P in keyof T]?: T[P] | MatchQuery<T[P]>
};

@sandersn
Copy link
Member Author

@arciisine for that small of a repro, there's nothing to cause the compiler to start checking the insides of the mapped type. As soon as you add some code that uses the mapped type you should see the crash.

@arciisine
Copy link

I actually have a larger minimal case, that doesn't cause it to happen. It appears to be something along the way, that I'm going to try to narrow down and see if I can hit that a minimal repro.

Btw, I have many versions of the recursive mapped type that do not throw any errors, but this one in specific seems to be problematic.

@mhegazy mhegazy added this to the TypeScript 2.7 milestone Oct 11, 2017
@sandersn
Copy link
Member Author

Fixed by #17912

@sandersn
Copy link
Member Author

Note that the fix in #17912 is specifically the deletion of if (isGenericMappedType(t)) { return emptyObjectType; } at the end of computeBaseConstraint. It looks like I will probably not delete this code in #17912, so this bug will remain open.

@sandersn
Copy link
Member Author

The call to isGenericMappedType might be correct if it would always return, but it indirectly calls getResolvedBaseConstraint(type) from within a call to getResolvedBaseConstraintType(type), so the process never makes progress.

I have a fix that clones isGenericMappedType and its callees inside of getResolvedBaseConstraint and replaces the recursive call, which fixes the bug in an ugly way.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jan 11, 2018
@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
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.

3 participants