The answer above actually gets the type union of all non-array elements of a multi-level array.
In other words
Flatten<[1,[2,'a',['b']]]>
will give you a union type of 1, 2, 'a', and 'b'
const foo: Flatten<[1,[2,'a',['b']]]> = 'b'; // OK
const bar: Flatten<[1,[2,'a',['b']]]> = 'c'; // Error: Type '"c"' is not assignable to type '1 | 2 | "a" | "b"'
Technically the inference is unnecessary there, if that's you're goal:
type Flatten<T> = T extends Array<unknown> ? Flatten<T[number]> : T
I don't really consider this the type of flattening an array, but `Array<Flatten<ArrType>>` would be. And this would actually be comparable to the builtin Array.prototype.flat type signature with infinite depth (you can see the typedef for that here[1], but this is the highest level of typescript sorcery)
My solution was for flattening an array with a depth of 1 (most people using Array.prototype.flat are using this default depth I'd wager):
In other words
will give you a union type of 1, 2, 'a', and 'b' Technically the inference is unnecessary there, if that's you're goal: I don't really consider this the type of flattening an array, but `Array<Flatten<ArrType>>` would be. And this would actually be comparable to the builtin Array.prototype.flat type signature with infinite depth (you can see the typedef for that here[1], but this is the highest level of typescript sorcery)My solution was for flattening an array with a depth of 1 (most people using Array.prototype.flat are using this default depth I'd wager):
The type I provided would match those semantics: [1]: https://github.com/microsoft/TypeScript/blob/main/src/lib/es...