Repository navigation
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
TypeScript Version: 2.9.2
Search Terms: generic function pipe contextual type wrong overload
Code
These examples are stripped down versions of Node's parse function in the querystring module and Lodash's values and fromPairs functions.
These are issues we ran into when trying to use these functions with a pipe function.
declare function pipe<A, B, C>(ab: (a: A) => B, bc: (b: B) => C): (a: A) => C;
{
type ParsedUrlQuery = { [index: string]: string };
declare function parsedUrlQueryString(str: string): ParsedUrlQuery;
declare function parsedUrlQueryString<T extends {}>(str: string): T;
declare const stringToString: (s: string) => string;
// $ExpectType (a: string) => ParsedUrlQuery
// but got (a: string) => {}
// Note: if we comment out the last overload for `parsedUrlQueryString`, we get the expected
// type.
const fn = pipe(
stringToString,
parsedUrlQueryString,
);
}
{
declare function values<T>(object: { [index: string]: T } | null | undefined): T[];
declare function values<T extends object>(object: T | null | undefined): Array<T[keyof T]>;
declare function values(object: any): any[];
declare const stringDictToStringDict: (
sd: { [index: string]: string },
) => { [index: string]: string };
// $ExpectType (a: { [index: string]: string; }) => string[]
// but got (a: { [index: string]: string; }) => any[]
// Note: if we comment out the last two overloads for `values`, we get the expected type.
const fn = pipe(
stringDictToStringDict,
values,
);
}
{
declare function fromPairs<T>(pairs: [string, T][] | null | undefined): { [index: string]: T };
declare function fromPairs(pairs: any[][] | null | undefined): { [index: string]: any };
// $ExpectType (a: {}) => { [index: string]: number; }
// but got (a: {}) => { [index: string]: any; }
// Note: if we comment out the last overload for `fromPairs`, we get the expected type.
const fn = pipe(
({ }: {}): [string, number][] => [['foo', 1]],
fromPairs,
);
}Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Activity
dardino commentedon Jul 13, 2018
Hi, I have a similar problem:
Playground
OliverJAsh commentedon Jul 17, 2018
Hey Mohamed Hegazy (@mhegazy), any idea if this is a bug or design limitation? Hopefully my examples demonstrate the frequency of this issue.
mhegazy commentedon Jul 19, 2018
In Gabriele (@dardino)'s example the overloads do not include the implementation signature, so there is no overload that accepts
(string | number)[] | ((k: string, v: any) => any), only ones that accept(string | number)[] | null or((k: string, v: any) => any) | null`.mhegazy commentedon Jul 19, 2018
In the OP:
first set of overloads, the second overload of
parsedUrlQueryStringseems meaningless. removing it should cause the compiler to infer the right type.Second and third sets, not sure what is going on, need to debug to figure it out
OliverJAsh commentedon Jul 19, 2018
I simplified the
parsedUrlQueryStringoverloads to demonstrate and isolate the issue—the full example does require that overload.dardino commentedon Jul 20, 2018
Mohamed Hegazy (@mhegazy) Sure, but i think this is a bit limiting thing.
in fact I can write this to compile correctly: (that is very heavy)
I believe that is possible to make this assumption:
If all available overloads of a method can be called with current parameters types because they are compatible then the compiler can accept the call without errors:
OliverJAsh commentedon Jul 20, 2018
Gabriele (@dardino) This issue is concerning overloads when used with a
pipefunction, which doesn't appear to be the case with your issue. Perhaps you could open a new issue to avoid conflating the issue being tracked here?dardino commentedon Jul 21, 2018
ok
[-]Wrong overloads are used for generic functions inside pipe[/-][+]Wrong overloads are used for generic functions inside `pipe`[/+]Erikvv commentedon Sep 26, 2018
Looks like a duplicate of #10957
weswigham commentedon Oct 2, 2018
Ref DefinitelyTyped/DefinitelyTyped#28592 which seems to have been motivated by this.
OliverJAsh commentedon Feb 13, 2019
Revisiting this. This isn't specific to generic composed functions. I believe the issue can be narrowed down to:
Any ideas Wesley Wigham (@weswigham)? Does this help?
[-]Wrong overloads are used for generic functions inside `pipe`[/-][+]Incorrect overload is used for generic functions inside `pipe`[/+][-]Incorrect overload is used for generic functions inside `pipe`[/-][+]Incorrect overload is used for composed function `pipe`[/+][-]Incorrect overload is used for composed function `pipe`[/-][+]Incorrect overload is used for composed function inside `pipe`[/+]RyanCavanaugh commentedon Aug 23, 2019
This is definitely #10957 - overloads aren't considered during generic inference
typescript-bot commentedon Aug 25, 2019
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
typescript-bot commentedon Aug 28, 2019
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
typescript-bot commentedon Aug 31, 2019
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
typescript-bot commentedon Sep 3, 2019
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
typescript-bot commentedon Sep 6, 2019
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
typescript-bot commentedon Sep 9, 2019
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
jack-williams commentedon Sep 9, 2019
You may rest now bot.