The below describes what I'd like to do:
interface Customer {
name: string;
age: number;
}
const getCustomer = () => {
return {
name: 'bob',
age: 42
}
}
export const getValue = (key: keyof Customer): typeof Customer[key] => {
const customer = getCustomer();
return customer[key];
};
but the TS compiler doesn't like the line typeof Customer[key]. I could make this any or string | number, but I should be able to get the type from this. For example, if I write:
getValue('name') what does it return? You know without any runtime information, it returns a string. What if I write getValue('age')? A number of course. getValue(someInputVal as any) might return string or number, but all of this is perfectly available at compile time. So - is there a way to get this from typescript?