3 ms·
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 ca
by dandare 12y ago
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.
- lmm 12y agoGenerics 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;
- xxgreg 12y agoMy 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;