Suppose I have an array of pairs as shown below:
const attributes = [
['Strength', 'STR'],
['Agility', 'AGI'],
['Intelligence', 'INT']
// ..
] as const;
I aim to create a structure like this:
const attributesMap = {
Strength: {
name: 'Strength',
shortName: 'STR',
},
Agility: {
name: 'Agility',
shortName: 'AGI',
},
// ..
};
This structure should be fully typed, ensuring that accessing attributesMap.Strength.shortName
will return "STR"
.
How can we achieve this level of typing?
The approach I've devised so far involves using the following type definition. However, it falls short in properly constraining the shortName
property to a specific value:
type mapType = {
[P in typeof attributes[number][0]]: {
name: P;
shortName: typeof attributes[number][1];
};
};