打字稿:重载函数的返回类型 [英] Typescript: ReturnType of overloaded function

查看:39
本文介绍了打字稿:重载函数的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ReturnType 给出的类型似乎取决于编写重载签名的顺序

The type given by ReturnType seems to depend on the order the overload signatures are written

function applyChanges1(input: string): number
function applyChanges1(input: number): string
function applyChanges1(input: number | string): number | string {
  return typeof input === "number" ? input.toString() : input.length
}

function applyChanges2(input: number): string
function applyChanges2(input: string): number
function applyChanges2(input: number | string): number | string {
  return typeof input === "number" ? input.toString() : input.length
}

type Ret1 = ReturnType<typeof applyChanges1> // string
type Ret2 = ReturnType<typeof applyChanges2> // number

它似乎采用了最后一个重载签名的返回类型,这似乎很随意.我期望 Ret1Ret2 都是 string |号码.这种行为有原因吗?

It seems to take the return type of the last overload signature which seems quite arbitrary. I was expecting both Ret1 and Ret2 to be string | number. Is there a reason for this behaviour?

推荐答案

正如 Matt McCutchen 所指出的,这是 ReturnType 以及一般条件类型和多个重载签名的限制.

As Matt McCutchen points this is a limitation of ReturnType and in general conditional types and multiple overload signatures.

然而,我们可以构造一个类型,该类型将返回最多任意数量的重载的所有重载返回类型:

We can however construct a type that will return all overloaded return types for up to an arbitrary number of overloads:

function applyChanges1(input: string): number
function applyChanges1(input: number): string
function applyChanges1(input: number | string): number | string {
return typeof input === "number" ? input.toString() : input.length
}

function applyChanges2(input: number): string
function applyChanges2(input: string): number
function applyChanges2(input: number | string): number | string {
return typeof input === "number" ? input.toString() : input.length
}


type OverloadedReturnType<T> = 
    T extends { (...args: any[]) : infer R; (...args: any[]) : infer R; (...args: any[]) : infer R ; (...args: any[]) : infer R } ? R  :
    T extends { (...args: any[]) : infer R; (...args: any[]) : infer R; (...args: any[]) : infer R } ? R  :
    T extends { (...args: any[]) : infer R; (...args: any[]) : infer R } ? R  :
    T extends (...args: any[]) => infer R ? R : any


type RetO1 = OverloadedReturnType<typeof applyChanges1> // string | number 
type RetO2 = OverloadedReturnType<typeof applyChanges2> // number | string

上述版本最多可用于 4 个重载签名(无论它们是什么),但可以轻松(如果不是很漂亮)扩展到更多.

The version above will work for up to 4 overload signatures (whatever they may be) but can easily (if not prettily) be extended to more.

我们甚至可以用同样的方式得到可能参数类型的联合:

We can even get a union of possible argument types in the same way:

type OverloadedArguments<T> = 
    T extends { (...args: infer A1) : any; (...args: infer A2) : any; (...args: infer A3) : any ; (...args: infer A4) : any } ? A1|A2|A3|A4  :
    T extends { (...args: infer A1) : any; (...args: infer A2) : any; (...args: infer A3) : any } ? A1|A2|A3 :
    T extends { (...args: infer A1) : any; (...args: infer A2) : any } ? A1|A2  :
    T extends (...args: infer A) => any ? A : any


type RetO1 = OverloadedArguments<typeof applyChanges1> // [string] & [number]
type RetO2 = OverloadedArguments<typeof applyChanges2>  // [number] & [string]

这篇关于打字稿:重载函数的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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