Last active
January 24, 2017 23:53
-
-
Save valtron/5688638 to your computer and use it in GitHub Desktop.
typescript-unsound-generics.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Box<T> { constructor(public value: T) {} } | |
class A {} | |
class B { foo() {} } | |
var bb : Box<B> = new Box<B>(null); | |
// This typechecks (it shouldn't; T is used both in a co- and contra-variant position, so it's invariant)... | |
var ba : Box<A> = xb; | |
ba.value = new A(); | |
// ... which causes an error at runtime, here | |
bb.value.foo(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment