We have many different games and while I could write a function that uses a bunch of if statements to get the proper model I would like to solve it bit cleaner.
I currently have the following code:
const { game, id } = request.params as { game: string; id: string }
const gameMap = {
'd3' : prisma.d3Profile
}
if (!Object.keys(gameMap).includes(game)) {
return { error: 'unknown game' }
}
// @ts-ignore
const profile = gameMap[game].findUnique({
where: {
id: parseInt(id)
}
})
if (profile) {
return profile
}
return { error: 'profile not found'}
Everything works but I would love to solve this problem without resorting to using ts-ignore. Does anyone have an idea how to solve this?
CodePudding user response:
Explicitly define type of request.params
const gameMap = {
'd3' : prisma.d3Profile
}
const { game, id } = request.params as { game: keyof typeof gameMap; id: string }
This way typescript has a way to infer it correctly