I am trying to create a unique function that can manipulate values of a nested Record, based on the collection name. However, in my current implementation, I am facing difficulty in attaining the value type:
type Player = { name:string }
type Point = { x:number, y:number }
const collections = {
players: {} as Record<string, Player>,
positions: {} as Record<string, Point>,
}
type Collections = typeof collections;
function handleEntity<
Key extends keyof Collections,
Col extends Collections[Key],
Val extends Col[keyof Col],
>(name:Key, value:Val){
switch(name){
case 'positions': value.x++ // <-- Not valid :(
}
}
Can you tell me why TypeScript is not able to infer Val in this scenario? Is there any possible workaround?