Skip to content

Wrong overload signature chosen for super class method #22584

Closed
@jdforsythe

Description

@jdforsythe

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions