1

I fail to understand why these two array-flattening types aren't equivalent :

type FlatArray1<T> = Array<T extends Array<infer U> ? U : T>
type FlatArray2<T> = T extends Array<infer U> ? Array<U> : Array<T>

type A = FlatArray1<boolean> // boolean[]
type B = FlatArray2<boolean> // false[] | true[]

Link to playground

Thanks for your help.

ostrebler
  • 940
  • 9
  • 32
  • does [this](https://stackoverflow.com/questions/55382306/typescript-distributive-conditional-types) answer your question? In `FlatArray2`, `T` is "naked". wrapping it will make it equivalent to `FlatArray1`: [playground](https://tsplay.dev/WvpzrN) – chrisbajorin Apr 18 '21 at 03:35
  • Ah yes, I see. I makes sense. – ostrebler Apr 18 '21 at 03:39
  • For some additional clarification, `boolean` is implemented under the hood as `type boolean = true | false`. Since it's a union, it's being distributed. Had your type not been a union, they would be equivalent. – chrisbajorin Apr 18 '21 at 03:45
  • Yes, I get it. You can post this as an answer. – ostrebler Apr 18 '21 at 03:46

0 Answers0