TypeScript

Microsoft have released a typed superset of JavaScript called TypeScript. I spent a sizable portion of my life looking at types and JavaScript, in particular Inferring types for JavaScript programmes. I still find the area interesting, and it’s nice to see this new and exciting work. I wonder if TypeScript has a sound type system? I see already that someone on Twitter has pointed out that arrays are mutable and covariant:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class A {}

class B extends A {
    foo () {  }
}

class C extends A { }

var b: B[] = [new B()];
var a: A[] = b;
a[0] = new C();

var boom: B = b[0];
boom.foo();  // boom points to an instance of C which has no foo method!

When you run the above you’ll get an error stating that Object #<C> has no method ‘foo’. The Java type system also has covariant arrays, but would prevent the assignment on line 11 at runtime:

1
2
3
4
5
6
7
8
9
10
11
public class A {
        public static void main(String args[]) {
         B[] b = new B[2];
         A[] a = b;
         a[0] = new C(); // Runtime exception: Exception in thread "main" java.lang.ArrayStoreException: C
        }
}

class B extends A {}

class C extends A { }
 
   

Comments

   
 

Copyright © 2014 - Christopher Lyon Anderson. Powered by Octopress