Been shipping TS for years but i keep seeing conditional types in libraries and honestly they feel like black magic. Saw this in a type utility yesterday:
type Flatten<T> = T extends Array<infer U> ? Flatten<U> : T
Like i get the basic syntax, T extends X means "if T is assignable to X" but the recursive part scrambled my brain. Why would you need that? What's the actual problem this solves vs just using generics normally.
Also seeing distributive conditional types mentioned everywhere and apparently they do something different in mapped types? I don't even know what's distributing.
Feel like i'm missing something obvious here. Everyone acts like this is normal typescript and i'm just pattern matching on types like some kind of functional programmer. Give me the eli5. What are these actually for and when should i care.
No responses yet.