Show Menu
Cheatography

TypeScript Cheat Sheet (DRAFT) by

For developers who know Javascript and want to learn Typescript

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Basic Types

number
bool
string
any

Declar­ations

var x: any
Basic declar­ation
var x: {a:any; b:any;};
x is an object
var x: Foo;
x is instance of class Foo

Parameters

x:any
Basic parameter
x:() => string
Function that returns string.
x:{a:a­ny;­b:any;}
Object that contains a and b
x:Foo
Object that is a class of Foo
x?:any
Optional parameter
 

Function

function a(x:bool);
function a(x:nu­mber);
function a(x:an­y):­bool{
   return x%2 == 0;
}

a(2);

Basic Class

class Goose{
   a:n­umber;
   private b:bool;
   con­str­uct­or(x: number, y:bool = true){
      ­this.a = x;
      ­this.b = y;
   }
}

var x: Goose = new Goose(50);

Class Inheri­tance

class Suzy extends Goose{
   con­str­uct­or(­public c: string){
      ­sup­er(0, true);
   }
}

var y: Suzy = new Suzy("f­oo");
consol­e.l­og(y.c + " | " + y.a);

var z: Goose = new Suzy("b­ar")

Interface Example

interface Foo{
   a(b­:nu­mbe­r):­bool;
}

class Bar implements Foo{
   a(b­:nu­mber){
      ­return false;
   }
}

var x:Foo = new Bar();
 

Function Explin­ation

Overload functions with bool and number.

Create new function that takes a number x and returns a bool.


Execute function.

Class Explin­ation

Create a new class.
Public attribute.
Private attribute.
constr­uctor with attributes x and y
y is optional and defaulted to true.




Instan­tiate with x as 50 and use default for y

Inheri­tance Explin­ation

New class that extends Goose
constr­uctor. creates a public var c
calls inherited constr­uctor.



New instance of the class.
Accessing public attribute c and a.

Making a new Goose using class Suzy.

Interface Explin­ation

Create a new Interface
   Which much have function a


Create a new class that implement Foo
   Imp­lement everything in Foo




Create a new instance of type Foo