Handle heterogeneous arrays in flat and flatMap #24579
Labels
Domain: lib.d.ts
The issue relates to the different libraries shipped with TypeScript
ES Next
New featurers for ECMAScript (a.k.a. ESNext)
Help Wanted
You can do this
Suggestion
An idea for TypeScript
Milestone
The typings for both flat and flatMap work ok for homogeneous arrays but do not handle tuple types well. Consider the current typings (with rename):
Using conditional types and
infer
we can start to see a solution like:Which works great for array types and handles some tuple types better, but sacrifices a type error on the depth parameter when the array isn't nested that much, and more importantly, doesn't handle heterogeneous tuple cases, e.g.
[ [string, number], boolean]
should flatten to[string, number, boolean]
. As far as I can tell, this is not possible without variadic kinds. Additionally, this implementation would benefit hugely from a way to reference a type recursively, e.g. https://github.com/Microsoft/TypeScript/issues/6230 (I'm not the only one to notice this issue trying to type this API).The text was updated successfully, but these errors were encountered: