1 of 7

The Type System in TypeScript

Masayuki Ioki

2 of 7

TypeScript

  • One implementation of AltJS
  • Based on ECMAScript 6
  • TypeScript = Class based OO + Static Typing + Javascript

3 of 7

Static Typing in Typescript

  • Structural Typing
    • not Nominal Typing
    • Soft Typing may be one of `Structural Typing`
  • (Weak) type inference
  • erase type information at runtime

4 of 7

Structural Typing

  • Like DuckTyping
  • Type is a set of methods

class A {

x:number;

}

class B {

x:number;

}

var a:A = new B(); // O.K.

5 of 7

the type of variables

// strict

var x : number = 1;

// o.k. the type of x is number.

var x = 0;

// ??

var x;

x = 0;

x = ‘a’; // o.k.

6 of 7

(weak) type inference

// strict

var fnc = (x:number) : number { x }

// o.k.

var fnc = (x:number) { x }

// error

var fnc = (x:number) { if(x==0) return 0; else return ‘a’; }

// ??

var fnc = (x:number) { if(x==0) return 0; }

7 of 7

Objects have types

  • they are the structural type.
  • {a: 0} => {a: number}

// error

var fnc = (x:number) => { if(x==0) return {a:0}; else return {b:0}; }

// ???

var fnc = (x:number) => { var o={};if(x==0) o['a']=0; return o; }