I'm looking for a way to make Typescript guess the return type of my function according to the function arguments.
function fn<T extends object>(a: T, b: T, property: keyof T): any {
return a[property] ?? b[property];
}
I want to remove any to get the proper return type.
interface A {
foo?: string;
}
const a: A = { foo : 'bar' };
const b: A = {};
const a = fn(a, b, 'foo'); // it should get the string type from inference
I looked at ReturnType<T> by using it like this ReturnType<typeof T[property]> but it seems not supported by Typescript. I don't known if it's feasible?