Skip to content

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

Closed

Description

πŸ”Ž 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

Activity

  1. jcalz commented on Jan 28, 2025

    @jcalz
    Contributor

    duplicate of #41164

  2. AaronNGray commented on Jan 28, 2025

    @AaronNGray
    Author

    Looks semantically totally different to me.

  3. jcalz commented on Jan 28, 2025

    @jcalz
    Contributor

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

  4. AaronNGray commented on Jan 28, 2025

    @AaronNGray
    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.

  5. jcalz commented on Jan 28, 2025

    @jcalz
    Contributor

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

  6. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions