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

Wrong overload signature chosen for super class method #22584

Closed
jdforsythe opened this issue Mar 14, 2018 · 2 comments
Closed

Wrong overload signature chosen for super class method #22584

jdforsythe opened this issue Mar 14, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@jdforsythe
Copy link

jdforsythe commented Mar 14, 2018

TypeScript Version: 2.7.2

Search Terms:
overload signature super union
Code

class Entity {
  id: number;
}

class BaseClass {
  constructor() { }

  testMethod(single: Entity): Entity;
  testMethod(multiple: Entity[]): Entity[];
  testMethod(singleOrMultiple: Entity | Entity[]): Entity | Entity[] {
    return singleOrMultiple;
  }

  testTwo(single: Entity): Entity;
  testTwo(multiple: Entity[]): Entity[];
  testTwo(singleOrMultiple: Entity | Entity[]): Entity | Entity[] {
    return singleOrMultiple;
  }
}

class SubClass extends BaseClass {
  testMethod(single: Entity): Entity;
  testMethod(multiple: Entity[]): Entity[];
  testMethod(singleOrMultiple: Entity | Entity[]): Entity | Entity[] {
    // Argument of type 'Entity | Entity[]' is not assignable to parameter of type 'Entity[]'.
    // Type 'Entity' is not assignable to type 'Entity[]'.
    // Property 'includes' is missing in type 'Entity'.
    return super.testMethod(singleOrMultiple);
  }

  testTwo(single: Entity): Entity;
  testTwo(multiple: Entity[]): Entity[];
  testTwo(singleOrMultiple: Entity | Entity[]): Entity | Entity[] {
    // if I manually narrow to array or not, the compiler doesn't complain
    if (Array.isArray(singleOrMultiple)) {
      return super.testTwo(singleOrMultiple);
    }
    else {
      return super.testTwo(singleOrMultiple);
    }
  }
}

Expected behavior:
Since the signatures are identical, I would expect the compiler to choose the proper overload of the super method

Actual behavior:
Compiler chooses the wrong overload signature.

Playground Link:

Related Issues:

@ghost
Copy link

ghost commented Mar 15, 2018

Duplicate of #14107

Note that you may manually fix this by writing:

testMethod(single: Entity): Entity;
testMethod(multiple: Entity[]): Entity[];
testMethod(singleOrMultiple: Entity | Entity[]): Entity | Entity[];
testMethod(singleOrMultiple: Entity | Entity[]): Entity | Entity[] {

The final signature is the implementation signature and isn't visible, so we have to duplicate it.

@ghost ghost marked this as a duplicate of #14107 Mar 15, 2018
@ghost ghost added the Duplicate An existing issue was already created label Mar 15, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants