Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange behavior of function which has overload in higher kind function. #32081

Closed
jituanlin opened this issue Jun 25, 2019 · 1 comment
Closed

Comments

@jituanlin
Copy link

consider bellow example:

interface Fn {
  (a: number): number;
  (a: string, b: number): number;
}

type HKF = <F extends (args: number) => any>(f: F) => Parameters<F>;

declare const fn: Fn;
declare const hkf: HKF;
// The `result` will be infer as [string,number], we expect [number]
const result = hkf(fn);

In this example, our intend is the hkf function will choose the first signature of Fn,
because only the first signature match the type constraint that F extends (args: number) => any.

I test it in many times and found that,
Typescript always choose the last one signature of overload function in this case.

@jack-williams
Copy link
Collaborator

I test it in many times and found that,
Typescript always choose the last one signature of overload function in this case.

Yes, this is the intended behaviour as described in the pull request that implements conditional type inference.

#21496

When inferring from a type with multiple call signatures (such as the type of an overloaded function), inferences are made from the last signature (which, presumably, is the most permissive catch-all case). It is not possible to perform overload resolution based on a list of argument types (this would require us to support typeof for arbitrary expressions, as suggested in #6606, or something similar).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants