Given a union type:
type X = ["a", number] | ["b", string] | ["c"]
It would be nice to have a mapping from the first element in the tuple to the tuple type. I think a common sense way to do this would be to generate a map using the key in syntax:
type Y = {
[T[0] in X]: T
}
However, that doesn't work. I think this could be very powerful for unions of objects as well rather than having to hardcode the relationship yourself.
My suggestion meets these guidelines:
Already possible:
type X = ["a", number] | ["b", string] | ["c"];
type Y = {
[K in X[0]]: Extract<X, {0: K}>
}
Oh interesting. You can do it the other way! It appears you can do this with objects as well.
type X = {0:"a", 1:number} | ["b", string] | ["c"];
type Y = {
[K in X[0]]: Extract<X, {0: K}>
}
This is a really neat trick. Thanks for sharing. I wish this was more discoverable!