TypeScript:是否可以获取泛型函数的返回类型? [英] TypeScript: Is it possible to get the return type of a generic function?

查看:624
本文介绍了TypeScript:是否可以获取泛型函数的返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从某个模块中导出了一个函数,如下所示:

I have exported a function from some module that looks like:

export function MyFunc<A>() {
    return {
        foo: (in: A) => void
    }
}

现在,在其他模块中,我希望能够讨论MyFunc的不同返回类型.由于我没有导出类型,因此我将使用typeof来获取要给定值MyFunc的类型. 理想情况下,我会执行以下操作:

Now, in some other module, I want to be able to talk about the different return types of MyFunc. Since I didn't export the type, I'll use typeof to get hold of the type I want given the value MyFunc. Ideally I would do the following:

import { MyFunc } from "mymodule";
type MyFuncReturned<A> = ReturnType<typeof MyFunc<A>>;

function foo(): MyFuncReturned<string> {
   // ...
}

麻痹,这行不通; typeof只能传递一个值,并且不喜欢我尝试指定该值的通用类型.

Hrmph, this doesn't work; typeof can only be passed a value and doesn't like my attempt to specify the generic type of that value.

我能做的最好的就是说服TypeScript从我创建的值中推断MyFunc的特定类型,然后为它们提供单独的类型别名,例如:

The best I can do is convincing TypeScript to infer specific types of MyFunc from values I've created, and then giving them individual type aliases, eg:

const myFuncStringReturn = MyFunc<string>();
type MyFuncStringReturn = typeof myFuncStringReturn;

为避免为了获取类型信息而实际运行MyFunc,我可以将其隐藏在函数后面并在其上使用ReturnType:

To avoid actually running MyFunc just to get the type info, I can hide it behind a function and use ReturnType on it:

const myFuncStringReturn = () => MyFunc<string>();
type MyFuncStringReturn = ReturnType<typeof myFuncStringReturn>;

const myFuncBoolReturn = () => MyFunc<bool>();
type MyFuncBoolReturn = ReturnType<typeof myFuncBoolReturn>;

这给了我一种方式,一次谈论MyFunc的不同返回类型,但是

This gives me a way of, one type at a time, talking about the different return types of MyFunc, but it

  • 涉及TS可以从中推断出的实际运行时代码.
  • 不要让我从更笼统的意义上谈论MyFunc.

我能想到的唯一适当的"解决方案是在声明MyFunc时复制一堆类型信息:

The only "proper" solution I can come up with is duplicating a bunch of type info when I declare MyFunc:

export function MyFunc<A>(): MyFuncReturns<A> {
    return {
        foo: (in: A) => void
    }
}

export type MyFuncReturns<A> = {
    foo: (in: A) => void
}

但是现在当我更改MyFunc时,我必须确保保持MyFuncReturns与它同步.

But now as I change MyFunc, I have to make sure to keep MyFuncReturns in sync with it.

是否可以通过给定导出值MyFunc来获得像MyFuncReturns<A>这样的类型,而不必添加运行时代码或在上面添加样板?

Is there any way I can get hold of a type like MyFuncReturns<A> given just our exported value MyFunc, without having to add runtime code or add the boilerplate above?

推荐答案

有人建议允许将typeof与任意表达式一起使用,以允许诸如获取特定类型参数的泛型函数的返回类型之类的事情(请参见此处

There is a proposal to allow using typeof with arbitrary expressions to allow things like getting the return type of a generic functions for a specific type argument (see here and here)

今天可以使用的更通用的变通方法是使用具有与该函数的返回类型相关联的字段的通用类.然后,我们可以提取类的字段.因为对于类,我们可以在类型表达式中指定泛型类型参数,所以我们可以提取返回类型的泛型形式:

A more generic workaround that works today is to use a generic class with a field that is tied to the return type of the function. We can then extract the field of the class. Since for classes we can specify generic type parameters in type expressions we can extract the generic form of the return type:

export function MyFunc<A>() {
  return {
    foo: (os : A) => {}
  }
}

class Helper <T> {
  Return = MyFunc<T>()
}
type FuncReturnType<T> = Helper<T>['Return']
type ForBool = FuncReturnType<boolean> //  {foo: (os: boolean) => void;}
type ForString = FuncReturnType<string> //  {foo: (os: string) => void;}

注意:如果您具有A的约束,则需要在HelperFuncReturnType中的T上重复这些约束,这是不可避免的.

Note If you have constraints of A you will need to duplicate those on T in Helper and FuncReturnType, that is unavoidable unfortunately.

这篇关于TypeScript:是否可以获取泛型函数的返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆