From the following Union type:
type Modifier =
| Date
| RangeModifier
| BeforeModifier
| AfterModifier
| BeforeAfterModifier
| DaysOfWeekModifier
| FunctionModifier
| undefined;
...I've built the following out of the type names:
const MODIFIER_NAMES = [
'undefined',
'Date',
'RangeModifier',
'BeforeModifier',
'AfterModifier',
'BeforeAfterModifier',
'DaysOfWeekModifier',
'FunctionModifier',
] as const;
type ModifierNamesTuple = typeof MODIFIER_NAMES;
type ModifierNames = ModifierNamesTuple[ number ];
I need to strictly map the names in ModifierNames
to their corresponding type in Modifier
, so that I can use those in a JS Map object. Something like...
const modifierMap = new Map<ModifierNames, Modifier>();
...but with the intended type safety between the key and its value. For example:
// For these modifiers (notice their type)...
const rangeModifier: Modifier = {
from: new Date(),
to: new Date()
}
const beforeModifier: Modifier = {
before: new Date()
}
// The following should be invalid
modifierMap.set('BeforeModifier', rangeModifier);
// While the following should be valid
modifierMap.set('RangeModifier', rangeModifier);
How can I achieve this?