-
-
Notifications
You must be signed in to change notification settings - Fork 549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add UnionToUnorderedTuple
type
#686
Conversation
Thanks for contributing. It would be great if you could check out the previous attempt at adding this type and address the feedback given there (if relevant): #167 |
hi @sindresorhus , thanks for pointing this out. Yes indeed this implementation has exactly the same issues as during previous attempt. The problem is in TypeScript itself. It doesn't guarantee that tuple will always preserve order of it's elements. Closing this for now |
hi @sindresorhus, just found this microsoft/TypeScript#44116 (comment)
So, maybe we shouldn't expect tuple to be ordered in the same way as union? Personally, I would have this |
Maybe just renaming this to |
Yeah. I would go with |
Bump :) |
👍 |
f1cad77
to
122ea9d
Compare
hi @sindresorhus, @mkovel. I started refactoring this and discovered, that there are other conceptual problems with this type. Not sure what should be the correct result in this case type T = UnionToUnsortedTuple<boolean | 'a'>; // [false, true, 'a'] There are many more issues & attempts to solve them here: microsoft/TypeScript#13298 |
@kopach Hi, honestly I didn't dive into it deeply. I just reused this code to solve the issue. Maybe it will be helpful. export type UnionToIntersection<U> = (
U extends never ? never : (arg: U) => never
) extends (arg: infer I) => void
? I
: never;
export type UnionToTuple<T> = UnionToIntersection<
T extends never ? never : (t: T) => T
> extends (_: never) => infer W
? [...UnionToTuple<Exclude<T, W>>, W]
: []; |
This is by far one of the most amazing types I've ever seen. It's exactly what I needed, and I don't really care much about the undefined behavior aspect. It's just for type checking and cli configuration. Thank you so much. |
I think it's still worth adding this type, but this PR is not moving forward, so closing for now: #819 |
No description provided.