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

type inference error in multi-olverloads function body #25865

Closed
dardino opened this issue Jul 23, 2018 · 6 comments
Closed

type inference error in multi-olverloads function body #25865

dardino opened this issue Jul 23, 2018 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@dardino
Copy link

dardino commented Jul 23, 2018

TypeScript Version: 2.9

Search Terms:
method overloads when pipe

Code

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 b = (value?: C1 | C2 | C3 | null | undefined): string => {
    if (value instanceof C1) return a(value);  // C1 : no error OK!
    if (value instanceof C2) return a(value);  // C2 : no error OK!
    return value ? value.P3 : ""; // : no error OK!
}

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? !!KO!!
    return value ? value.P3 : ""; // no error OK!
}

Expected behavior:
no errors in the function "c" because is exactly the same as function "b"

Actual behavior:
error in function "c":

Argument of type 'C1 | C2' is not assignable to parameter of type 'C2'.
  Type 'C1' is not assignable to type 'C2'.
    Property 'P2' is missing in type 'C1'.
(parameter) value: C1 | C2

Playground Link:
PlayGround

Related Issues:
#25637
#25791
#25826

@j-oliveras
Copy link
Contributor

From FAQs: A function or a method implementation signature is not part of the overloads.

Add another overload to function a:

function a(value: C1 | null): string;
function a(value: C2 | null): string;
function a(value: C1 | C2 | null): string;
function a(value: C1 | C2 | null): string {
    return "";
}

@AlCalzone
Copy link
Contributor

@j-oliveras that was going to be my first response too, but then I thought the error is a bit illogical.

In the line where the error occurs, we know that value is either a C1 or a C2. For both options, an overload of a exists. There's probably some design limitation or intent behind this, but why should ((value: C1) => string) | ((value: C2) => string) not be callable with a C1 | C2?

@j-oliveras
Copy link
Contributor

@AlCalzone In this case, it is duplicate of #14107 and #1805.

@AlCalzone
Copy link
Contributor

And possibly related: #7294

type Foo = ((val: number) => any) | ((val: string) => any);
let f: Foo;
f(1); // error: Type Foo lacks a call signature

@mhegazy
Copy link
Contributor

mhegazy commented Jul 23, 2018

indeed duplicate of #14107

@mhegazy mhegazy added the Duplicate An existing issue was already created label Jul 23, 2018
@dardino
Copy link
Author

dardino commented Jul 31, 2018

@j-oliveras I am not the owner of ".d.ts" file that describes the overloads. I can't add the overloads as you suggest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants