Hacker News new | past | comments | ask | show | jobs | submit login

Generics are bivariant in TypeScript and so unsound:

    class Base {}
    class Derived1 extends Base {}
    class Derived2 extends Base {}
    
    class Box<T> {
      constructor(public value: T) {}
    };

    var a: Derived1 = new Derived1();
    var b: Box<Base> = new Box<Base>(a);
    var c: Box<Derived2> = b;
    var d: Derived2 = c.value;



TypeScript is structurally typed; there's nothing in your example that suggests that Derived1 and Derived2 have different types, instances of each are the same type.


Point, but add a differently-named field to each and it will still compile.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: