This website's docs

Learn how to use and customise this website to your liking.

TypeScript Cheatsheet

Basic Types

let isDone: boolean = false;
let decimal: number = 6;
let color: string = "blue";

Array

let list: number[] = [1, 2, 3];
let list: Array<number> = [1, 2, 3];  // generic array type

Tuple

let x: [string, number];
x = ["hello", 10];  // OK

Enum

enum Color {Red, Green, Blue}
let c: Color = Color.Green;

Any

let notSure: any = 4;
notSure = "maybe a string instead";

Void

function warnUser(): void {
    console.log("This is my warning message");
}

Null and Undefined

let u: undefined = undefined;
let n: null = null;

Never

function error(message: string): never {
    throw new Error(message);
}

Object

declare function create(o: object | null): void;
create({ prop: 0 });  // OK

Type assertions

let someValue: any = "this is a string";
let strLength: number = (<string>someValue).length;
let strLength: number = (someValue as string).length;

Interfaces

interface LabelledValue {
  label: string;
}
function printLabel(labelledObj: LabelledValue) {
  console.log(labelledObj.label);
}

Classes

class Greeter {
  greeting: string;
  constructor(message: string) {
    this.greeting = message;
  }
  greet() {
    return "Hello, " + this.greeting;
  }
}

Generics

function identity<T>(arg: T): T {
  return arg;
}

Remember to check out the TypeScript documentation for a full list of features and how to use them.