Skip to content

Instantly share code, notes, and snippets.

@t0yv0
Last active December 15, 2015 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t0yv0/5242309 to your computer and use it in GitHub Desktop.
Save t0yv0/5242309 to your computer and use it in GitHub Desktop.
// Example 1: `any` allows unsafe casts
var x : any = 1;
x();
// Example 2: subtyping on functions allows unsafe casts
interface I1 { x: number; }
interface I2 { x: number; y: number; }
function f(g: (x: I1) => void) { g({x: 1}); }
f(function (x: I2) { console.log(x.y); })
// Example 3: type system ignores closing over `this`:
class C {
n: number;
constructor () {
this.n = 0;
}
next() {
var k = this.n;
this.n = k + 1;
return k;
}
}
function fc(g: () => number) {
console.log(g());
console.log(g());
console.log(g());
}
var c = new C();
fc(c.next);
fc(c.next);
fc(c.next);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment