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

Can someone give me an example of TypeScript declaration that is not "sound"?

I thought that

"It guarantees that (1) an expression that has a static type T can never evaluate to a value that is not a T, and that (2) evaluation cannot run into unexpected runtime type errors."

is already supported in TypeScript.




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.


My understanding is that gradually typed code, i.e. using "any", introduces unsoundness.

    var s : String = "bar";
    var a : any = s;
    var i : int = a;
Not sure, but I think Typescript's assignability rules, which use structural typing, also introduce unsoundness.

    class A { foo() { print('A'); } }
    class B { foo() { print('B'); } }
    
    var a : A = new A();
    var b : B = a;




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

Search: