Skip to content

Circular Generic Type Transforms to New mutation Circular Generic Types #61686

Closed as not planned
@wesleyolis

Description

@wesleyolis

πŸ” Search Terms

Circular Generic Type Transforms to New mutation Circular Generic Types
Recursive Types

βœ… Viability Checklist

⭐ Suggestion

I would like to propose enhanced support for Generic Types that references things themself, Circular in nature, to improve the ability to represent real world API and scenarios which current typing system is not able to do, or has hard code limits 50.

I like to propose that in side a type generic type definition that a key wrapper 'Circular' be introduce, to help accelerate the lookup that would be need for this feature, else negate that just do it but performance knock could be big, as need to limit the scope.

I know one can do the background programming build the stack potentially in my mind to track this, ideally syntax below could be debated,
but improve recursive typing and transform of existing recursive types to stop infinite evaluation.

When evaluating an runtime object with a type, then the runtime object can easily lazy evaluate the recursive types on each iteration.

The only thing to not, that potentially not having Circular, you could break all existing hack transforms, have made ts nicely hackable, do all the cool things you can, but stopping them form being transformed, which another reason in my mind 'Circular' annotation would be needed.

type MyCircularType<TCore, TTerminate> = TCore & {
RecusiveFields: (TTerminate | Circular<MyCircularType<TCore, TTerminate>>) []
// this method above allows for a non uniform type inside the array, can keep alternating,
}

// Need to be totally seperate, as required a uniform type inside the array, want the same type for the whole array.
type MyCircularType2<TCore, TTerminate> = TCore & {
RecusiveFields: TTerminate [] | Circular<MyCircularType2<TCore, TTerminate>> []
}

type MyCircularType3<TCore> = TCore & {
RecusiveFields?: Circular<MyCircularType3<TCore>> []
}

Or the following syntax
type MyCircularType<TCore, TTerminate> = Circular<TCore & {
RecusiveFields: (TTerminate | MyCircularType<TCore, TTerminate>) []
// this method above allows for a non uniform type inside the array, can keep alternating,
}>

// Need to be totally separate, as required a uniform type inside the array, want the same type for the whole array.
type MyCircularType2<TCore, TTerminate> = Circular<TCore & {
RecusiveFields: TTerminate [] | MyCircularType2<TCore, TTerminate> []
}>

type MyCircularType3<TCore> = Circular<TCore & {
RecusiveFields?: MyCircularType3<TCore>> []
}>

Then with this a lot of types all get transformed all the time and typescript tends to fall over with the 50 limit of recursive depth evaluation.

You can build a stack and added type definitions to it, to look up and detect recursive types. The circular, would make it simpler for you if this type was more complex and maybe want to reduce Mutiple nested circular types, one could better control the behavior.
#61685

When defined a type transform of a circular type, to which would inspect for transformation, you can terminate the transform expansion on 'Circular' generic wrapper, then potentially transform the recursive type at that point and mutate it to a new Circular type form if necessary, preventing you from having to evaluating to infinite upon hitting circular.

I know there more details to this, but I believe you can do this and solve this issue once and for all.
Obviously need evaluated it with the large typescript language, something I don't have time for right now...

πŸ“ƒ Motivating Example

We need better support for recursive/Circular typing, that works and to really complete the experience now and be able to type all data structures properly.

πŸ’» Use Cases

  1. What do you want to use this for?
  2. What shortcomings exist with current approaches?
  3. What workarounds are you using in the meantime?

Metadata

Metadata

Assignees

No one assigned

    Labels

    UnactionableThere isn't something we can do with this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions