The Problem
I had the requirement to convert an enum to a mapped type.
So the following enum:
enum Animals {
CAT,
DOG,
PARROT,
SHEEP,
SALMON
}
Would result in the following mapped type:
type AnimalsToMappedType = {
0: "CAT";
1: "DOG";
2: "PARROT";
3: "SHEEP";
4: "SALMON";
}
The Solution
Through trial and error (in the TS Playground) I came up with the following solution which solves this:
type AnimalsToMappedType = {
[K in (keyof typeof Animals) as typeof Animals[K]]: K
}
But there are a couple of things i don’t understand.
The Question
1). The following is my first attempt, but the output also includes readonly [x: number]: number;
type AnimalsToMappedType = {
[K in keyof typeof Animals]: K
}
/*
type AnimalsToMappedType = {
readonly [x: number]: number;
readonly CAT: "CAT";
readonly DOG: "DOG";
readonly PARROT: "PARROT";
readonly SHEEP: "SHEEP";
readonly SALMON: "SALMON";
}
*/
My initial thought was to remove this using a conditional type, so i first wrapped keyof typeof Animals
in parenthesis with the intention of writing (keyof typeof Animals extends ...)
but to my surprise, the parenthesis alone solved the problem.
Why do the parenthesis alone solve this?
2). At this point i was stuck with the following and couldn’t work out how to convert the keys to the indexes.
type AnimalsToMappedType = {
[K in (keyof typeof Animals)]: K
}
/*
type AnimalsToMappedType2 = {
CAT: "CAT";
DOG: "DOG";
PARROT: "PARROT";
SHEEP: "SHEEP";
SALMON: "SALMON";
}
*/
I added the following as typeof Animals[K]
, not expecting it to work, but was pleasantly surprised when it did.. although logically, i’m not sure why.
type AnimalsToMappedType = {
[K in (keyof typeof Animals) as typeof Animals[K]]: K
}
How does typeof Animals[K]
result in the numerical index?