Skip to content

Assignability issue with function types and and subclass argument#5673

@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

classMammal{}classWhaleextendsMammal{swim(duration:number){/* some code */}}functionswim10(w:Whale){w.swim(10);}typemammalFunc=(m:Mammal)=>void;functioneach(m:Mammal[],f:(Mammal)=>void){m.forEach(f);}varf1:mammalFunc=swim10;// Problem : this should not be possiblevarm1=newMammal();f1(m1);// this compiles, but will fail at runtime because m1 has no swim() methodeach([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