Answer the question
In order to leave comments, you need to log in
How to make a value object generic?
Good day to all! I'm learning typescript while I'm just learning and trying myself, in general, typing is something new for me, and I have a question.
There is such a code
type myObjectTypes = {
name: string;
number: number
}
type myObjectKeys = keyof myObjectTypes;
const myObject: myObjectTypes = {
name: 'Alex',
number: 1337
}
const myFunction = (key: myObjectKeys): <???> => myObject[key as string];
Answer the question
In order to leave comments, you need to log in
Link
const myFunction = <K extends keyof MyObjectTypes>(key: K): MyObjectTypes[K] => myObject[key];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question