sindresorhus / type-fest

A collection of essential TypeScript types
Creative Commons Zero v1.0 Universal
13.87k stars 527 forks source link

`UnionToUnorderedTuple` type #819

Open sindresorhus opened 6 months ago

sindresorhus commented 6 months ago

I think it's worth adding this as long as we clearly document its downsides.

Initial attempt in https://github.com/sindresorhus/type-fest/pull/686

Upvote & Fund

Fund with Polar

voxpelli commented 6 months ago

We basically want to do what's described here, right? https://stackoverflow.com/a/55128956

Any other techniques?

voxpelli commented 6 months ago

Basically:

  1. Convert union values to return values of functions
  2. UnionToIntersection to make it an intersection
  3. Intersection will be treated as a function overload from which the base return value (the last one in the original union) can be easily extracted
  4. Add that extracted last value to the the tuple result we want
  5. Repeat everything again with the remaining values that has not yet been extracted, adding one more item to the tuple for every iteration