-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Incorrect overload is used for composed function inside pipe
#25637
Comments
Hi, I have a similar problem: function a(value: any, replacer?: (string | number)[] | null, space?: string | number): string;
function a(value: any, replacer?: ((k: string, v: any) => any) | null, space?: string | number): string;
function a(value: any, replacer?: ((k: string, v: any) => any) | (string | number)[] | null, space?: string | number): string {
return "";
}
var b = (value: any, replacer?: ((k: string, v: any) => any) | (string|number)[] | null, space?: string | number): string => {
if (replacer instanceof Array || typeof replacer === "function")
return a(value, replacer, space); // Why ERROR ??? function "a" supports both Array and function as 2nd parameter
} |
Hey @mhegazy, any idea if this is a bug or design limitation? Hopefully my examples demonstrate the frequency of this issue. |
In @dardino's example the overloads do not include the implementation signature, so there is no overload that accepts |
In the OP: |
I simplified the |
@mhegazy Sure, but i think this is a bit limiting thing. var b = (value: any, replacer?: ((k: string, v: any) => any) | (string|number)[] | null, space?: string | number): string => {
if (replacer instanceof Array)
return a(value, replacer, space);
if (typeof replacer === "function")
return a(value, replacer, space);
} I believe that is possible to make this assumption: class C1 { P1: string; }
class C2 { P2: string; }
class C3 { P3: string; }
function a(value: C1 | null): string;
function a(value: C2 | null): string;
function a(value: C1 | C2 | null): string {
return "";
}
var c = (value?: C1 | C2 | C3 | null | undefined): string => {
if (value instanceof C1 || value instanceof C2)
return a(value); // C1 | C2 --> not null or undefined, there is no reasons to give an error...
// is a(C1) a valid call? YES
// is a(C2) a valid call? YES
// why error?
return value ? value.P3 : "";
}
var b = (value?: C1 | C2 | C3 | null | undefined): string => {
if (value instanceof C1) return a(value); // C1
if (value instanceof C2) return a(value); // C2
return value ? value.P3 : "";
} |
@dardino This issue is concerning overloads when used with a |
ok |
pipe
Looks like a duplicate of #10957 |
Ref DefinitelyTyped/DefinitelyTyped#28592 which seems to have been motivated by this. |
Revisiting this. This isn't specific to generic composed functions. I believe the issue can be narrowed down to: {
declare const pipe: {
<A, B, C>(ab: (a: A) => B, bc: (b: B) => C): (a: A) => C;
};
declare const myFn: {
(p: string): string;
(p: any): number;
};
// Expected type: `(a: string) => string`
// Actual type: `(a: string) => number`
// Note: if we comment out the last overload for `myFn`, we get the expected
// type.
const fn = pipe(
(s: string) => s,
myFn,
);
} Any ideas @weswigham? Does this help? |
pipe
pipe
pipe
pipe
pipe
pipe
This is definitely #10957 - overloads aren't considered during generic inference |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
5 similar comments
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
You may rest now bot. |
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 thequerystring
module and Lodash'svalues
andfromPairs
functions.These are issues we ran into when trying to use these functions with a
pipe
function.The text was updated successfully, but these errors were encountered: