Skip to content

Instantly share code, notes, and snippets.

@puffnfresh
Created October 1, 2012 17:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save puffnfresh/3813040 to your computer and use it in GitHub Desktop.
Save puffnfresh/3813040 to your computer and use it in GitHub Desktop.
Covariant arrays. Blurgh.
// Compiles fine even though the code is broken.
// This is because arrays are covariant and mutable.
//
// TypeScript team: Please fix either of those things. Preferably mutability :)
class Animal {}
class Snake extends Animal {
slither() {
alert("Slithering!")
}
}
class Horse extends Animal {}
var a: Snake[] = [new Snake()]
var b: Animal[] = a
b[0] = new Horse()
var notASnake: Snake = a[0]
notASnake.slither()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment