Skip to content

Latest commit

 

History

History
33 lines (25 loc) · 3.75 KB

File metadata and controls

33 lines (25 loc) · 3.75 KB

Union to Tuple hard #union #tuple #infer

by Sg @suica

Take the Challenge

Implement a type, UnionToTuple, that converts a union to a tuple.

As we know, union is an unordered structure, but tuple is an ordered, which implies that we are not supposed to preassume any order will be preserved between terms of one union, when unions are created or transformed.

Hence in this challenge, any permutation of the elements in the output tuple is acceptable.

Your type should resolve to one of the following two types, but NOT a union of them!

UnionToTuple<1>           // [1], and correct
UnionToTuple<'any' | 'a'> // ['any','a'], and correct

or

UnionToTuple<'any' | 'a'> // ['a','any'], and correct

It shouldn't be a union of all acceptable tuples...

UnionToTuple<'any' | 'a'> // ['a','any'] | ['any','a'], which is incorrect

And a union could collapes, which means some types could absorb (or be absorbed by) others and there is no way to prevent this absorption. See the following examples:

Equal<UnionToTuple<any | 'a'>,       UnionToTuple<any>>         // will always be a true
Equal<UnionToTuple<unknown | 'a'>,   UnionToTuple<unknown>>     // will always be a true
Equal<UnionToTuple<never | 'a'>,     UnionToTuple<'a'>>         // will always be a true
Equal<UnionToTuple<'a' | 'a' | 'a'>, UnionToTuple<'a'>>         // will always be a true

Back Share your Solutions Check out Solutions

Related Challenges

10・Tuple to Union 11・Tuple to Object 55・Union to Intersection 472・Tuple to Enum Object 3188・Tuple to Nested Object