Typescript infinite recursion reasoning

7

Check this typescript 4.2 snippet I found somewhere (playground here):

type BlackMagic<T> = { [K in keyof T]: BlackMagic<T[K]> }

declare const foo: BlackMagic<{q: string}>;
declare const str: BlackMagic<string>;
declare const num: BlackMagic<12>;

I can't wrap my head around it. How does TS handle this? How is it not stuck in an infinite recursion? Specifically, in the case of str and num, hovering over the variables shows that TS is resolving the types to just string and 12. How's that even happening?

Share
Improve this question

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

flutter websocket connection issue