Skip to content

Shouldn't allow a class instance passed to function if it is the wrong type #12811

Closed
@eamodio

Description

@eamodio

TypeScript Version: 2.1.4

Code

Playground link

class Base {
    value: string;
    constructor(value: string) { this.value = value; }
}

class A extends Base {
    constructor(value: string) { super(value); }
}

class B extends Base {
    constructor(value: string) { super(value); }
}

function onlyA(o: A) { console.log("onlyA", o.value); }
function onlyB(o: B) { console.log("onlyB", o.value); }

let a = new A("a");
let b = new B("b");

onlyA(a); // good
onlyA(b); // bad
onlyB(b); // good
onlyB(a); // bad

Expected behavior:

I would expect the lines marked bad above to cause compile errors. I would expect this behavior when using interfaces, but not with classes.

Actual behavior:

Compiles without errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions