[英]TypeScript - ReturnType of a generic function not working
There is a feature in typescript called ReturnType<TFunction>
which allows you to infer the return type of a particular function, like so打字稿中有一个名为
ReturnType<TFunction>
的功能,它允许您推断特定函数的返回类型,如下所示
function arrayOf(item: string): string[] {
return [item]
}
However, I am having trouble using it with generic functions:但是,我在将它与泛型函数一起使用时遇到了问题:
function arrayOf<T>(item: T): T[] {
return [item]
}
type R = ReturnType<typeof arrayOf> // R = {}[]
type R = ReturnType<typeof arrayOf<number>> // syntax error
// etc.
Using the top answer from Typescript ReturnType of generic function , I have tried this: (by the way this is not a duplicate, it is different as the solution and question do apply to this case)使用通用函数的 Typescript ReturnType的最佳答案,我试过这个:(顺便说一下,这不是重复的,它是不同的,因为解决方案和问题确实适用于这种情况)
function arrayOf<T>(x: T): T[] {
return [x];
}
type GenericReturnType<R, X> = X extends (...args: any[]) => R ? R : never;
type N = GenericReturnType<number, <T>(item: T) => T[]>; // N = never
I have also tried the following:我还尝试了以下方法:
type GenericReturnType<TGenericParameter, TFunction> = TFunction extends (...args: any[]) => infer R ? R : never;
type N = GenericReturnType<number, <T>(item: T) => T[]>; // N = {}[]
as well as也
type GenericReturnType<TGenericParameter, TFunction> = TFunction extends <T>(...args: any[]) => infer R ? R : never;
type N = GenericReturnType<number, <T>(item: T) => T[]>; // N = {}[]
and和
type GenericReturnType<TGenericParameter, TFunction> = TFunction extends <T extends TGenericParameter>(...args: any[]) => infer R ? R : never;
type N = GenericReturnType<number, <T>(item: T) => T[]>; // N = {}[]
and also this还有这个
type GenericReturnType<TGenericParameter, TFunction> = TFunction extends (arg: TGenericParameter) => infer R ? R : never;
type N = GenericReturnType<number, <T>(item: T) => T[]>; // N = {}[]
and this和这个
type GenericReturnType<TGenericParameter, TFunction> = TFunction extends <U extends TGenericParameter>(arg: U) => infer R ? R : never;
type N = GenericReturnType<number, <T>(item: T) => T[]>; // N = {}[]
as well as也
type x = (<T>(item: T) => T[]) extends <T>(arg: T) => infer R ? R : never // x = {}[]
and finally最后
type x = (<T>(item: T) => T[]) extends (arg: number) => infer R ? R : never // x = {}[]
But none of them yield the much wanted type of number[]
但是它们都没有产生非常想要的
number[]
类型number[]
So, my question is, is there any way to create something similar to the builtin ReturnType
which works for functions with generic parameters, given the types of the generic parameters?所以,我的问题是,有没有办法创建类似于内置
ReturnType
东西,它适用于具有泛型参数的函数,给定泛型参数的类型? (aka a solution to the above problem) (又名上述问题的解决方案)
I have the same problem as you and after countless workarounds not working, I found a somewhat "hack".我和你有同样的问题,在无数次的解决方法都不起作用之后,我发现了一个有点“黑客”的问题。 However, this might not be convenient for all cases:
但是,这可能不适用于所有情况:
Similar to the answer given by Matt, the point is to have a dummy function, but without rewriting it.类似于 Matt 给出的答案,重点是要有一个虚拟函数,但不要重写它。 Instead of writing a dummy rewrite of your function, write a dummy function that returns a dummy usage of your function.
与其编写函数的虚拟重写,不如编写一个返回函数的虚拟用法的虚拟函数。 Then, get the resulting type with
ReturnType
:然后,使用
ReturnType
获取结果类型:
function arrayOf<T>(item: T): T[] {
return [item];
}
const arrayOfNumber = () => arrayOf<number>(1);
type N = ReturnType<typeof arrayOfNumber>;
As of TS 3.7 , unless I haven't done my research right, it still isn't possible to achieve the desired results without finding a workaround.从TS 3.7 开始,除非我没有正确完成研究,否则在没有找到解决方法的情况下仍然不可能达到预期的结果。 Hopefully, we can get something that looks like this:
希望我们能得到这样的东西:
function arrayOf<T>(item: T): T[] {
return [item];
}
type N = GenericReturnType<typeof arrayOf>; // N<T>
Given the type arguments to the function, not currently, if you follow the chain of links to Getting the return type of a function which uses generics .给定函数的类型参数,而不是当前,如果您遵循获取使用泛型的函数的返回类型的链接链。 Given the argument types, you can write a non-generic dummy function that passes arguments of those types and then check its return type:
给定参数类型,您可以编写一个非通用虚拟函数来传递这些类型的参数,然后检查其返回类型:
function dummy(n: number) { return arrayOf(n); }
type N = ReturnType<typeof dummy>;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.