Reputation: 6373
Let's say I have a tuple
type MyTuple = [string,number];
Now, I would like to get the union of all numeric keys for this tuple, i.e. 0 | 1
. I can do this:
type MyKeys = Exclude<keyof MyTuple, keyof unknown[]>;
which will return the type "0" | "1"
, but that is a union of string literals. Is it possible to get a union of number literals instead?
Upvotes: 0
Views: 38
Reputation: 6373
Ah, so I found the solution. Here it is:
type MyTuple = [string, string, string];
type KeysOfTuple = Exclude<Partial<MyTuple>['length'], MyTuple['length']>;
// KeysOfTuple = 0 | 2 | 1
Upvotes: 2