Skip to content

Circular Reference with Generic Parameterization - error TS2456: Type alias 'Result' circularly references itself. #61062

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
AaronNGray opened this issue Jan 28, 2025 · 5 comments
Labels
Not a Defect This behavior is one of several equally-correct options

Comments

@AaronNGray
Copy link

AaronNGray commented Jan 28, 2025

πŸ”Ž Search Terms

Circular Reference, Generic Parameterization, error TS2456: Type alias 'Result' circularly references itself.

πŸ•— Version & Regression Information

Failing on Main 32ebd28

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAggdiAPAFShAHsCcAmBnKAeQCMArCAY2AD4oBeKAbwCgo2oBtAawhAC48wAE4BLOAHMAunyjIA3MwC+cqM2ahIUAEoQ8AVwA2welEGiJUAD7bdh4B0lXYCRDv1HqKgPRe0QoQD2QrIAygBMACwArABsMsjg0ACGBiJJBADkbnYZUBQiQhSGSUIGIFBCEABmEJVwFLpQIsB4EAZVAHTMQA

πŸ’» Code

Fails :-

type Any<T extends Object> = {
    [key:string]: T;
}; 

type Result = string | Result[] | Any<Result>; // error TS2456: Type alias 'Result' circularly references itself.

Works :-

type ResultObject = {
    [key:string]: Result;
}; 

type Result = string | Result[] | ResultObject;

Fails :-

type ResultObject = {
    [key:string]: Result;
}; 

type ResultArray<T> = T[];
 
type Result = string | ResultArray<Result> | ResultObject;  // error TS2456: Type alias 'Result' circularly references itself.

It looks like it is a Generic Parameterization issue.

πŸ™ Actual behavior

Throwing Error on compilation error TS2456: Type alias 'Result' circularly references itself.

πŸ™‚ Expected behavior

Should not throw an error and demonstrates the type system is incomplete.

Additional information about the issue

No response

@jcalz
Copy link
Contributor

jcalz commented Jan 28, 2025

duplicate of #41164

@AaronNGray
Copy link
Author

Looks semantically totally different to me.

@jcalz
Copy link
Contributor

jcalz commented Jan 28, 2025

Any<T> is the same as Record<string, T> and you have the same problem with it.

@AaronNGray
Copy link
Author

So basically there is no support for recursive parameterized types and mutually recursive parameterized types in TypeScript.

This is a crazy limitation as so many programming patterns I use use this.

I need to find my feet with TypeScripts code base and see if I can implement this.

@jcalz
Copy link
Contributor

jcalz commented Jan 28, 2025

You might want to read through the other issue thoroughly, especially the linked PR at #57293.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Defect This behavior is one of several equally-correct options
Projects
None yet
Development

No branches or pull requests

3 participants