Skip to content

Assignability issue with function types and and subclass argument #5673

Closed
@olivr70

Description

@olivr70

Hi
I stumbled on a strange behaviour which in my understanding is a bug (in Node with Typescript 1.6 and reproduced in the online Playground)

B is a subclass of A
It is possible to assign a variable F of type "function with A argument" with a value "function with a B argument".
This means that when calling F with a value of type A, the concrete function called will use some specific properties of B which are missing, leading to a runtime error

Olivier

class Mammal {
}
class Whale extends Mammal { 
    swim(duration:number) { /* some code */ } 
}

function swim10(w:Whale) { w.swim(10); }

type mammalFunc = (m:Mammal) => void;
function each(m:Mammal[], f:(Mammal) => void) { m.forEach(f); }

var f1:mammalFunc = swim10;
    // Problem : this should not be possible
var m1 = new Mammal();

f1(m1);
    // this compiles, but will fail at runtime because m1 has no swim() method
each([m1], swim10);
    // this compiles, but will also fail at runtime

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions