Skip to content

Circular reference in default type parameter crashes compiler #16221

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
jcalz opened this issue Jun 2, 2017 · 1 comment
Closed

Circular reference in default type parameter crashes compiler #16221

jcalz opened this issue Jun 2, 2017 · 1 comment
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@jcalz
Copy link
Contributor

jcalz commented Jun 2, 2017

TypeScript Version: 2.3 and up

Code

class SelfReference<T = SelfReference<string>> {}

Expected behavior:
The compiler either accepts it (so that new SelfReference() has the type SelfReference<SelfReference<string>>) or rejects it (by explicitly disallowing a circular reference in default type parameters).

Actual behavior:
The compiler goes into an infinite loop that exceeds the recursion limit:

RangeError: Maximum call stack size exceeded
  at resolveNameHelper (typescript\lib\typescript.js:26480:35)
  at resolveName (typescript\lib\typescript.js:26478:20)
  at resolveEntityName (typescript\lib\typescript.js:27128:26)
  at getTypeFromTypeReference (typescript\lib\typescript.js:31874:54)
  at getTypeFromTypeNode (typescript\lib\typescript.js:32724:28)
  at getDefaultFromTypeParameter (typescript\lib\typescript.js:31032:66)
⋮
@mhegazy mhegazy added the Bug A bug in TypeScript label Jun 2, 2017
@jcalz
Copy link
Contributor Author

jcalz commented Jun 3, 2017

Thinking about it more, I'd say the expected behavior is that

class SelfReference<T = SelfReference<string>> {}

should be accepted. Whatever is parsing SelfReference<string> should not care about resolving the default type parameter value, since the type is explicitly given as string.

On the other hand

class SelfReference<T = SelfReference> {}

should probably be rejected, since it seems to require the existence of the infinite type SelfReference<SelfReference<SelfReference<…>>>.

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

No branches or pull requests

3 participants