Created
September 23, 2017 00:03
-
-
Save undeadcat/ac53178d4bd15a960f36c2fa2a7a95f2 to your computer and use it in GitHub Desktop.
TypeScript is unsound
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 Base { | |
} | |
class Derived extends Base { | |
y:string | |
} | |
//pretend we have variance | |
class Collection1<T>{ | |
accept(value: T) { } | |
} | |
var derivedCollection: Collection1<Derived> = new Collection1<Base>(); | |
derivedCollection.accept(new Derived()) | |
class Collection2<T>{ | |
getResult(): T { return null } | |
} | |
var baseCollection2: Collection2<Base> = new Collection2<Derived>(); | |
var result: Base = baseCollection2.getResult() | |
//but adding an indexer breaks everything | |
class Base { | |
} | |
class Derived extends Base { | |
y: string | |
} | |
class Collection3<T>{ | |
["string"]: T; | |
} | |
var derived: Collection3<Derived> | |
derived["foo"] = new Base(); | |
var base: Collection3<Base> | |
derived["foo"] = new Derived(); | |
//unsound | |
var original = new Map<string, Derived>(); | |
var viaBase: Map<string, Base> = original; | |
var viaDerived: Map<string, Derived> = original; | |
viaBase["fff"] = new Base(); | |
console.log(viaDerived["fff"]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment