-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Higher order function inference breaks with multiple overloads #30369
Comments
In general we can't do overload resolution at the same time as inference and will usually be working with either the first or the last overload, depending on the situation |
Thanks for the explanation. How come |
In your example code: createSelector(
(
// Expected `state` param type to be inferred as `{}` (fallback)
// Actual type: `any`
state,
props: Props,
) => props.foo, The
If you comment the overload type signature, it will work well, because the compiler can infer it's type from (
// Expected `state` param type to be inferred as `{}` (fallback)
// Actual type: `any`
state,
props: Props,
) => props.foo, So, in your example code, the |
It is true, however, is there any solution? const f = <I, R>(f: (x: I) => R): ((x: I) => R) => (x: I) => f(x);
interface Fn {
(x: number): number;
(x: string): string;
}
declare const fn: Fn;
// type of `r` is (x:string)=>string, the `union type` property is lost
const r = f(fn); The result type is make me upset. I found a solution for the above case: const f = <I, R ,F extends (x: I) => R>(f: F): F => {
return ((x: I) => f(x) )as unknown as F
};
interface Fn {
(x: number): number;
(x: string): string;
}
declare const fn: Fn;
// type of `r` is Fn, `union type` property is preserve
const r = f(fn);
// work
const v = r('1') But it work just for the above specify case, it not solve the problem:
|
This comment has been minimized.
This comment has been minimized.
> Sobrecarga las funciones de response-utils para que solo haya que llamar a una. El problema es que los tipos todavía no funcionan del todo bien (véase microsoft/TypeScript#30369 (comment)). > Añade tests para el response-utils.
TypeScript Version: 3.3.3333, 3.4.0-dev.20190313
Search Terms: higher order function generic param inference any reselect
Code
This is an issue I found whilst using the popular Reselect library, however I believe it is a bug with TypeScript rather than the type definitions. Here is a minimal repro.
The text was updated successfully, but these errors were encountered: