Turn every overload of a function type into a union of ordinary function types. One conditional with four infer pairs does it, no cascade of fallbacks needed.
An overloaded function type carries a stack of call signatures, and TypeScript hands you exactly one of them.
OverloadToUnion<F> pulls the whole stack apart: one plain, single-signature function type per overload, joined into a union. Browser and DOM typings are full of these stacks, and anything that wants to wrap, filter or re-map such an API has to get the signatures out first. The built-in Parameters<F> and ReturnType<F> are no help, since both see only the final overload.
type Tab = { id: number; url?: string }
declare function queryTab(callback: (user: Tab) => void): void
declare function queryTab(id: number): Tab
declare function queryTab(name: string): Promise<Tab>
type Q = OverloadToUnion<typeof queryTab>
// ((callback: (user: Tab) => void) => void)
// | ((id: number) => Tab)
// | ((name: string) => Promise<Tab>)Implement a type OverloadToUnion that transforms all overloads of a
function into a union.
View on GitHub: https://tsch.js.org/20909
Change the following code to make the test cases pass (no type check errors).
/* _____________ Your Code Here _____________ */
type OverloadToUnion<F> = any
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '../helpers'
type Tab = {
id: number
url?: string
}
declare function queryTab(callback: (user: Tab) => void): void
declare function queryTab(id: number): Tab
declare function queryTab(name: string): Promise<Tab>
type Details = {
code?: string
}
type InsertCSSFunc = {
(details: Details): Promise<void>
(details: Details, callback?: () => void): void
(tabId: number, details: Details): Promise<void>
(tabId: number, detailGet access to all 200+ challenges, including every medium, hard, and extreme one.
One-time payment, lifetime access. 30-day money-back guarantee.
The solution in full:
type OverloadToUnion<F> = F extends {
(...args: infer A1): infer R1
(...args: infer A2): infer R2
(...args: infer A3): infer R3
(...args: infer A4): infer R4
}
?
| ((...args: A1) => R1)
| ((...args: A2) => R2)
| ((...args: A3) => R3)
| ((...args: A4) => R4)
: neverShorter than the challenge deserves, and three things in it are worth slowing down for.
The pattern you would reach for first captures nothing but the last signature:
type Last = typeof queryTab extends (...args: infer A) => infer R ? [A, R] : never
// [[name: string], Promise<Tab>]An overloaded function is one type holding several call signatures, not a union of function types. When the compiler matches it against a target that has a single call signature, it does not merge the overloads, it picks one of them and infers from that, and the one it picks is the last. The callback and id overloads never reach A and R. Parameters<F> and ReturnType<F> are written against that same single-signature shape, which is why they inherit the blind spot.
Put more than one call signature in the target and the compiler pairs them up with the source overloads positionally, so each infer pair captures its own signature:
type Two = typeof queryTab extends {
(...args: infer A1): infer R1
(...args: infer A2): infer R2
}
? [R1, R2]
: never
// [Tab, Promise<Tab>]Two slots against three overloads gives you the last two. The pairing is aligned to the end of the list, so a target that is too narrow drops signatures off the front.
The usual next move is a cascade: try four signatures, fall back to three, then two, then one, and take the first shape that matches. It turns out you do not need it. A target with more slots than the source has overloads still matches, and the leftover slots at the front all bind to the first overload:
declare function one(a: string): 1
type Four = OverloadToUnion<typeof one>
// ((a: string) => 1) | ((a: string) => 1)
// | ((a: string) => 1) | ((a: string) => 1)
// which is just ((a: string) => 1)A union of four identical types is that type. The same collapse handles queryTab: A1/R1 and A2/R2 both capture the callback overload, the copy disappears, and three members come back out. One pattern therefore covers one, two, three and four overloads without a single fallback branch.
The slot count is a ceiling, not a guess. A five-overload function would lose its first signature, and the repair is more slots, not more branches.
Each captured A is a tuple such as [id: number], and (...args: A1) => R1 turns it back into a function type. That works because a rest parameter whose type is a fixed-length tuple is normalised into ordinary positional parameters: (...args: [id: number]) => Tab and (id: number) => Tab are the same type. The labels ride along in the tuple and keep the output readable, but a label carries no type identity, so Equal still matches the unlabelled expectation in the tests.
Optional parameters survive the round trip as well. (details: Details, callback?: () => void) => void infers as [details: Details, callback?: (() => void) | undefined], and rebuilding from that tuple restores the optional marker.
InsertCSSFunc is declared as an object type with four call signatures instead of repeated declare function lines. To the compiler those are the same construct, so the pattern does not care which spelling the source uses.callback? keep their optional marker, and the expected type spells it callback?: (() => void) | undefined, which is what an optional parameter means under strict.queryTab has three overloads against four slots, so the padded first slot produces a duplicate. The union collapses it and the result has exactly three members.F that is not callable at all, a string or a plain object, fails the check and lands on never rather than yielding a nonsense signature.This challenge is originally from here.
Track your progress through 200+ hands-on challenges. Free, sign in with GitHub.
Or start solving right away: explore all TypeScript challenges