/** * https://github.com/microsoft/TypeScript/issues/14107#issuecomment-1146738780 */ type OverloadProps = Pick; type OverloadUnionRecursive = TOverload extends ( ...args: infer TArgs ) => infer TReturn ? // Prevent infinite recursion by stopping recursion when TPartialOverload // has accumulated all of the TOverload signatures. TPartialOverload extends TOverload ? never : | OverloadUnionRecursive< TPartialOverload & TOverload, TPartialOverload & ((...args: TArgs) => TReturn) & OverloadProps > | ((...args: TArgs) => TReturn) : never; type OverloadUnion any> = Exclude< OverloadUnionRecursive< // The "() => never" signature must be hoisted to the "front" of the // intersection, for two reasons: a) because recursion stops when it is // encountered, and b) it seems to prevent the collapse of subsequent // "compatible" signatures (eg. "() => void" into "(a?: 1) => void"), // which gives a direct conversion to a union. (() => never) & TOverload >, TOverload extends () => never ? never : () => never >; // Inferring a union of parameter tuples or return types is now possible. export type OverloadParameters any> = Parameters>; export type OverloadReturnType any> = ReturnType>;