Description
π Search Terms
Circular Generic Type Transforms to New mutation Circular Generic Types
Recursive Types
β Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β 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
- What do you want to use this for?
- What shortcomings exist with current approaches?
- What workarounds are you using in the meantime?