Skip to content

Issue using union type with generic #2576

@stephanedr

Description

@stephanedr

Trying to convert function overloads to union type, I'm getting errors.
Here is a very simple code that shows the error:

class Animal { run() {} }
class Cat extends Animal {}
class Dog extends Animal {}

function run(a: Animal) {
    a.run();
}

function f<T extends Cat | Dog>(a: T) {
    a.run(); // OK.
    run(a); // Argument of type 'T' is not assignable to parameter of type 'Animal'.
}

I don't understand the difference TS makes between a.run() and run(a).

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions