I have a question regarding the use of Record in TypeScript. When I specify Record as the parameter type in a function, I encounter an error in my code because it does not allow different types.
type Keys = 'name' | 'qty';
const getValueByKey = <T = any>(items: Record<Keys, T>, key: Keys) => {
return items[key];
}
getValueByKey({ name: 'fulan', qty: 1}, 'name'); // Error: Type 'number' is not assignable to type 'string'.(2322)
The main aim is to determine the type of the value within the function.
Is there a way to make a Record accept values of varying types? Thank you