Skip to content

Overloaded function calling overloaded function #39885

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

Closed
jmezzera opened this issue Aug 3, 2020 · 2 comments
Closed

Overloaded function calling overloaded function #39885

jmezzera opened this issue Aug 3, 2020 · 2 comments

Comments

@jmezzera
Copy link

jmezzera commented Aug 3, 2020

TypeScript Version: 3.9.2

Search Terms:

Expected behavior:
Being able to call an overloaded function with the union of types derived from the overloading of another function. In the provided example, in C2Wrong, param can be either A or B (nothing else), and method method on child accepts both A or B. Is there any reason why I should check myself? Doing so results in nonsense redundant code C2Ok.

Actual behavior:
The compiler complains about the types.

Related Issues:

Code

class A {
    prop1: string;
    constructor(prop1: string){
        this.prop1 = prop1
    }
}

class B {
    prop2: string;
    constructor(prop2: string){
        this.prop2 = prop2
    }
}

interface i1 {
    method(a: A): number;
    method(b: B): number;
}

class C1 implements i1 {
    method(a: A): number;
    method(b: B): number;
    method(param: A | B): number {
        return 1;
    }
}

interface i2 {
    method(a: A): number;
    method(b: B): number;
}

class C2Wrong implements i2 {
    child: i1 = new C1();
    method(a: A): number;
    method(b: B): number;
    method(param: A | B): number {
        return this.child.method(param);
    }
}
class C2Ok implements i2 {
    child: i1 = new C1();
    method(a: A): number;
    method(b: B): number;
    method(param: A | B): number {
        if (param instanceof A){
            return this.child.method(param);
        }
        return this.child.method(param);
    }
}
Output
"use strict";
class A {
    constructor(prop1) {
        this.prop1 = prop1;
    }
}
class B {
    constructor(prop2) {
        this.prop2 = prop2;
    }
}
class C1 {
    method(param) {
        return 1;
    }
}
class C2Wrong {
    constructor() {
        this.child = new C1();
    }
    method(param) {
        return this.child.method(param);
    }
}
class C2Ok {
    constructor() {
        this.child = new C1();
    }
    method(param) {
        if (param instanceof A) {
            return this.child.method(param);
        }
        return this.child.method(param);
    }
}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

@DanielRosenwasser
Copy link
Member

Seems like a duplicate of #1805

@jmezzera
Copy link
Author

jmezzera commented Aug 4, 2020

Seems like. My bad!

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