-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
Crash with recursive conditional type during serialization #43529
Comments
I get a max call stack size error when hovering |
Thank you for your response. And sorry for the delay. Here you will found the example project. On src/query/index.ts, you will find the interfaces, types and functions that I use during development. The master branch contains two commits:
I hope I'm helping you with this. Otherwise, do not hesitate to ask for any additional information. |
Reduced case: export type CanBeExpanded<T extends object> = {
value: T
}
type Expand__<O, N, Depth> =
N extends Depth ?
unknown :
O extends CanBeExpanded<any> ?
Expand__<O['value'], N, Depth> :
O
export type UseQueryOptions<T> = Expand__<T, 4, 2> |
Hi @andrewbranch, could you please give me a temporary solution that I could use until the v4.3.1 is fixed? |
Try using types understandable by mere mortal humans? 😅 I don’t know for sure yet because I haven’t fixed it yet. Even once I understand exactly what’s going wrong, it may be difficult to apply that knowledge back to the massive types you have. |
ahah! I see your point! :) I have created a more simplified example that works. type KeysCanBeExpanded_<T = never, Depth extends number[] = number[], Deep2 extends number[] = number[]> =
// This is to limit the deep of subcalls, when T is a object.
4 extends Depth['length'] ? never :
// This is a global limit that prevent the infinite recursion.
// If you comment the next line (16) the KeysCanBeExpanded<T> (line 32) will go on infinite recursion.
8 extends Deep2['length'] ? never :
T extends object ?
// The first Depth is the cause of infinite recursion on KeysCanBeExpanded<T>
KeysCanBeExpanded_<T, Depth, [1,...Deep2]>
:
T
export type KeysCanBeExpanded<T> = KeysCanBeExpanded_<T, [], []> As I have written in the comments above if you comment the line As I was thinking the problem is related to some knowledge that I was missing. I don't understand why KeysCanBeExpanded is "executed". I really appreciate your help :) Here you can find the playground. |
@jBernavaPrah, I’ve had a chance to debug this and see what’s going wrong, which led me to a workaround for you. The problem comes when we’re trying to serialize these recursive types. One way that we can avoid serializing them is if we can substitute a type alias for them. So, the more you can break these large recursive types down into smaller pieces, the more we can substitute in the names of those individual pieces and break the loop. Here I’ve extracted one part of Of course, this is still a bug on our end, but this should get you unstuck in the meantime. |
Fixed by #43733 |
Bug Report
Hi guys,
I am not sure if it is a bug but it is a strange behaviour surely.
I have created some Types that help me during the development of my ReactJs app.
I can use those types separately and they show output result quickly and use the normal quantity of memory.
Although, when I try to reuse those types with other Types, the compilation of typescript on my ReactJs app is not completed and after some time the node that runs
yarn start
crashes because it has reached the maximal memory allowed (it can be 2Gb or 8Gb, it is just a matter of time before the crash). If I comment out those types, the ReactJs compilation is completed as expected.I think I have recreated the same behaviour also on Playground. In this case, the typescript worker used by Chrome will grow to 1.5Gb, without showing information on the popup about the Type.
I have created this Video to understand better what I mean by it.
On the playground I have put what I think is the relevant code from my project, to show this error.
🔎 Search Terms
typescript, recursion, JavaScript heap out of memory, ReactJs
🕗 Version & Regression Information
Playground and typescript npm 4.2.5 and Nightly.
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Every time I try to put together multiple Types, the yarn start (
react-scripts start
) is stuck on:Files successfully emitted, waiting for typecheck results...
and after the memory node reaches the maximal allowed (can be 2Gb or 8Gb) it crashes with:🙂 Expected behavior
My intention is to be able to use the recursions Type together with other types.
The text was updated successfully, but these errors were encountered: