Skip to content

Instantly share code, notes, and snippets.

@valtron
Last active January 24, 2017 23:53
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valtron/5688638 to your computer and use it in GitHub Desktop.
Save valtron/5688638 to your computer and use it in GitHub Desktop.
typescript-unsound-generics.ts
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