Typescript 获取使用泛型的函数的返回类型

Typescript 获取使用泛型的函数的返回类型,typescript,generics,typescript-generics,Typescript,Generics,Typescript Generics,免责声明:接下来是过度简化的函数,我知道它们是 无用的 函数thinger(thing:T):T{ 归还物; } const thing=thinger({a:“lol”}); 一件事; 上面的代码可以正常传输。但我需要将thinger的结果放入一个对象中 interface ThingHolder { thing: ReturnType<typeof thinger>; } const myThingHolder: ThingHolder = { thing:

免责声明:接下来是过度简化的函数,我知道它们是 无用的

函数thinger(thing:T):T{
归还物;
}
const thing=thinger({a:“lol”});
一件事;
上面的代码可以正常传输。但我需要将
thinger
的结果放入一个对象中

interface ThingHolder {
    thing: ReturnType<typeof thinger>;
}

const myThingHolder: ThingHolder = {
    thing: thinger({ a: "lol" }),
};
但是
typeof thinger
不是有效的typescript


如何获得基于泛型的不同返回类型的函数的返回类型?

我不妨将其放在一个答案中,尽管它看起来不能满足您的需要。TypeScript当前既没有,也没有。TypeScript中的泛型有点“肤浅”。据我所知,不幸的是,无法描述将类型参数插入泛型函数并检查结果的类型函数:

// doesn't work, don't try it
type GenericReturnType<F, T> = F extends (x: T) => (infer U) ? U : never

function thinger<T>(thing: T): T {
  return thing;
}

// just {}, Probably this would solve the problem. But you need to create a fake class. It works because classes are types and JS-runtime objects at the same time.

// generic function
// we want to get its result, but we cannot do it like
// ReturnType<typeof foo<T>()> // syntax error
const foo = <T,>(arg: T) => ({ test: arg });
// so, let's create a parametric class
class Wrapper<T> {
  // with the only method that uses our "foo"
  wrapped = (arg: T) => foo(arg);   
};
// due to the fact that class is a type we can use it as a type 
// with a generic parameter.
type GetFooResult<T> = ReturnType<Wrapper<T>['wrapped']>
type Bingo = GetFooResult<number>; // { test: number }
//不起作用,不要尝试
类型GenericReturnType=F扩展(x:T)=>(推断U)?U:从来没有
函数thinger(thing:T):T{
归还物;
}

//只是{},这可能会解决问题。但是你需要创建一个假类。这是因为类同时是类型和JS运行时对象

//泛型函数
//我们想得到它的结果,但我们不能这样做
//ReturnType//语法错误
常量foo=(arg:T)=>({test:arg});
//那么,让我们创建一个参数化类
类包装器{
//使用唯一使用“foo”的方法
包装=(arg:T)=>foo(arg);
};
//由于类是一种类型,我们可以将它用作一种类型
//使用泛型参数。
类型GetFooResult=ReturnType
键入Bingo=GetFooResult;//{test:number}

TS操场。基于。Thx到@Colin

你自己弄明白有什么不对<代码>接口ThingHolder{thing:T}
?正如我在文章开头的免责声明中所述,我意识到在这个过于简化的示例中我应该这样做,但我的实际用例并不是那么简单。我有一个函数,它接受许多泛型并返回比那些泛型更复杂的东西(但泛型仍然影响返回类型),我可以编写它,但它会非常冗长,每次函数更改时都需要重新编写。老实说,即使这是一个合理的解决方案,我仍然希望得到这个问题的答案。简短的回答是“TypeScript不能这样做”,因为它既没有,也没有。这不是我的功能,它是一个库的。我只对我的问题的答案感兴趣,这是一种获取返回类型受泛型影响的函数的返回类型的方法。或者,解释为什么这是不可能的就足够了。如果不可能,我有更简单的方法来解决我的用例。但是如果可能的话,我更愿意这样做。在你分享的链接中有一些有趣的阅读,谢谢!接受答案为(至少使用TS 2.8)似乎是不可能的:(实际上我刚刚发布了一个详细的解决方案,在这里可能也会有所帮助。在这里我发布了一个替代解决方案:
// doesn't work, don't try it
type GenericReturnType<F, T> = F extends (x: T) => (infer U) ? U : never

function thinger<T>(thing: T): T {
  return thing;
}

// just {}, Probably this would solve the problem. But you need to create a fake class. It works because classes are types and JS-runtime objects at the same time.

// generic function
// we want to get its result, but we cannot do it like
// ReturnType<typeof foo<T>()> // syntax error
const foo = <T,>(arg: T) => ({ test: arg });
// so, let's create a parametric class
class Wrapper<T> {
  // with the only method that uses our "foo"
  wrapped = (arg: T) => foo(arg);   
};
// due to the fact that class is a type we can use it as a type 
// with a generic parameter.
type GetFooResult<T> = ReturnType<Wrapper<T>['wrapped']>
type Bingo = GetFooResult<number>; // { test: number }