【发布时间】:2020-01-15 07:13:11
【问题描述】:
我正在寻找一种方法让 Typescript 根据函数参数猜测我的函数的返回类型。
function fn<T extends object>(a: T, b: T, property: keyof T): any {
return a[property] ?? b[property];
}
我想删除 any 以获得正确的返回类型。
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
我通过使用 ReturnType<typeof T[property]> 来查看 ReturnType<T>,但 Typescript 似乎不支持它。不知道是否可行?
【问题讨论】:
标签: typescript typescript-typings typescript-generics