Skip to content

Inconsistent type inference on overloaded function typesΒ #57351

Open
@jsalvata

Description

@jsalvata

πŸ”Ž Search Terms

type inference of overloaded functions

πŸ•— Version & Regression Information

The behaviour seemed to change between 4.0.5 and 4.1.5, but it was incorrect nonetheless.

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.0.5#code/C4TwDgpgBAwghgGwQIzgYwNYB4BKA+KAXigAoAnCAZwFcFgAuKHASiIIDcB7ASwBMBuALAAoUJCgB5MMG6cAdpSJQA3lDTzgEAB4MocOSCgBfIcJHawnMsChjoAQUog5aAGLUXM+VntRtmuV5FDww5TgB3OQBtAF0AGkk-HQhAxSkvBQT8JWURKCgAegKoAGVwuDBbAAsqaGBwzigEbjkqW0bKCDqaqAAzDzzSADoRuDIAc0pGKJGh+wT4JFRMXDwY5kYuPlN8klmxyenZ+ckFxBR0bHx1zZ4BERMREX7PWTkoMDJOAFtuSm5eiAfEkAkEoCEwpFYgkJCCUmD0m9KFk8CReoxHM43B40BkfDCUaxlEYnsIXri3n0AIwkOSMOTUb7ICBkBJoRiLC4rBlMll4DZQLb3Mk4jLU2n0xnM1lQTiMRHyZFqDnnZbYHnS-m3bbPUWU3o0-YTKZ6AyxIkksyicAOJwuCRkABynGAWFcBGIrjhqSgmJc7le3havRZUAA+idg6GwxIElGyOHsgB+WxkajQRi9RCdUwiT4-P4AkBoqnMXPW8QAFSpSj9aAdztddk4vWpeFM6gUNmAVMY1aUwDTEFMQA

πŸ’» Code

type Callback<R> = (result: R) => void;
type Options = { context: any };

export type AsyncFunction<A extends unknown[], O extends Options, R> = {
  // Swap these two lines to see the fun
  (...args: [...A, Callback<R>]): void;
  (...args: [...A, O, Callback<R>]): void;
};

function promisify<A extends unknown[], O extends Options, R>(f: AsyncFunction<A, O, R>) {}

function f1(n: number, c: Callback<number>): void;
function f1(n: number, o: Options, c: Callback<number>): void;
function f1(...args: any[]) {}

type AsyncOrNot<F> = F extends AsyncFunction<infer _A, infer _O, infer _R> ? true : false;

promisify(f1);

type T1 = AsyncOrNot<typeof f1>;
const t1: T1 = true;

πŸ™ Actual behavior

In version 4.0.5, either the inference in the function call or the inference in the conditional type succeeds, depending on the order in which the overloads are declared.
In all later versions up to and including 5.4.0 beta, the inference in the conditional type always fails.

πŸ™‚ Expected behavior

Type inference for the same type against the same generic type to always succeed or always fail, whether it is done for a generic function call or for evaluating the extends clause in a conditional type.

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Has ReproThis issue has compiler-backed repros: https://aka.ms/ts-reprosHelp WantedYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions