microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
99.88k stars 12.36k forks source link

Inconsistent type inference on overloaded function types #57351

Open jsalvata opened 6 months ago

jsalvata commented 6 months ago

🔎 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

rotu commented 6 months ago

I think this might be a clearer example of part of this bug. Namely, while the function type extends both signatures, it can only infer the last return type.

type PolyFun = ((x:number)=>number) & ((x:string)=>string)

type A1 = PolyFun extends ((x:number)=>infer Z) ? Z : null // null, expected number
//   ^?
type A2 = PolyFun extends ((x:number)=>number) ? number : null // number
//   ^?

type A3 = PolyFun extends ((x:string)=>infer Z) ? Z : null // string
//   ^?
type A4 = PolyFun extends ((x:string)=>string) ? string : null // string
//   ^?

Workbench Repro

typescript-bot commented 6 months ago

:wave: Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of this repro running against the nightly TypeScript.


Comment by @rotu

:warning: Assertions:

Historical Information
Version Reproduction Outputs
4.9.3, 5.0.2, 5.1.3, 5.2.2, 5.3.2

:warning: Assertions:

  • type A1 = null
  • type A2 = number
  • type A3 = string
  • type A4 = string

MajorLift commented 6 months ago

Another simple repro focusing on the inconsistent behavior of typeof vs. infer/Parameters<T>/ReturnType<T> on overloaded functions.

/** overload signature 1 */
function overloadedFunction(
  stringArg: string,
): Record<typeof stringArg, string>;

/** overload signature 2 */
function overloadedFunction(
  numberArg: number,
): Record<typeof numberArg, number>;

/** implementation signature */
function overloadedFunction(
  arg: string | number,
): Record<string, string> | Record<number, number> {
    return {};
}

/**
 * `typeof` only displays the overload signatures and omits the implementation signature (this is expected behavior).
 * 
 * type TypeOfKeyword = {
 *   (stringArg: string): Record<typeof stringArg, string>;
 *   (numberArg: number): Record<typeof numberArg, number>;
 * }
 */
type TypeOfKeyword = typeof overloadedFunction;
//    ^?

/**
 * `infer`, `Parameters<T>`, `ReturnType<T>` only use the last overload signature (this is unexpected behavior).
 * If the order of the overload signatures is changed or new signatures are added, these results change as well.
 * 
 * type InferKeyword = {
 *   parameters: [numberArg: number];
 *   returnValue: Record<number, number>;
 * }
 */
type InferKeyword = typeof overloadedFunction extends (...args: infer P) => infer R 
//   ^?
    ? { parameters: P; returnValue: R; } 
    : never;
type ParametersUtility = Parameters<typeof overloadedFunction>; // [numberId: number]
//   ^?
type ReturnTypeUtility = ReturnType<typeof overloadedFunction>; // { [x: number]: number; }
//   ^?

Workbench repro

MajorLift commented 6 months ago

There's also the question of what InferKeyword in the above example should evaluate to.

Currently, it would probably be an expression of the implementation signature, even though only overload signatures are supposed to be externally exposed.

type InferKeyword = {
  parameters: [arg: string | number];
  returnValue: Record<string, string> | Record<number, number>;
}

Ideally, there should be a way to get an accurate discriminated union with the overload signatures as members.

type InferKeyword = {
  parameters: [stringArg: string];
  returnValue: Record<string, string>;
} | {
  parameters: [numberArg: number];
  returnValue: Record<number, number>;
}

To achieve this, we may need a new syntax for infer that lets it infer both the parameters and return value of a function type, or more generally has a distributive property over generic arguments. e.g.

type NewInferSyntax = typeof overloadedFunction extends infer ((...args: P) => R)
    ? { parameters: P; returnValue: R } 
    : never;
typescript-bot commented 6 months ago

:wave: Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of this repro running against the nightly TypeScript.


Comment by @MajorLift

:warning: Assertions:

Historical Information
Version Reproduction Outputs
5.0.2, 5.1.3, 5.2.2, 5.3.2

:warning: Assertions:

  • type TypeOfKeyword = { (stringArg: string): Record; (numberArg: number): Record; }
  • type InferKeyword = { parameters: [numberArg: number]; returnValue: Record; }
  • type ParametersUtility = [numberArg: number]
  • type ReturnTypeUtility = { [x: number]: number; }

4.9.3

:warning: Assertions:

  • type TypeOfKeyword = { (stringArg: string): Record; (numberArg: number): Record; }
  • type InferKeyword = { parameters: [numberArg: number]; returnValue: Record; }
  • type ParametersUtility = [numberArg: number]
  • type ReturnTypeUtility = { [x: number]: number; }