Here is an object where I keep functions by key:
export const questSetHelper: { [key in QuestSetHelper]: (player: Player, payload: FlagGender | FlagOrigin | FlagAdventure) => void } = {
'setGender': setGender,
'setOriginCircumstance': setOriginCircumstance,
'setAdventureCircumstance': setAdventureCircumstance
}
I keep them as strings for easy handling from front-end to back-end passing strings that are used to determine which function to run. Each function handles only one of the payload types.
For example:
export const setOriginCircumstance = (player: Player, payload: FlagOrigin): void => {
if (player.quests['intro']?.flags)
player.quests['intro'].flags['origin_circumstance'] = payload
else
throw new Error('Quest: intro - failed setOriginCircumstance')
}
When all these were typed to string, I had no type errors of course, but I want to have each of these function parameters explicitly typed.
PROBLEM
Obviously each function can ultimately handle ONE of the questSetHelper types of payload, but I want to assume that each function will be called correctly and passed the correct type (i.e. FlagGender payload will always go to setGender).
Is there a better way to type this without changing the structure?
questSetHelper. If not, please explain why. typescriptlang.org/play?#code/…