home
TypeScript cheat sheet
JavaScript that scales.
print

TypeScript is a super-set of JavaScript that enforce the code quality. This cheat sheet summarizes all the syntax and features of the language.

Usage

npm install typescript

> tsc hello-world.ts

Primitives types

Any type (explicitly untyped)

let whatever: any = "anything";

Void type (null or undefined, use for function returns only)

function nothing(): void {}

String

let myString: string = "hello";

Number

let myNumber: number = 1;

Boolean

let mybool: boolean = true;

Arrays

Array of strings

string[] or Array<string>

Array of functions that return boolean

{(): boolean;}[] or Array<()=>boolean>

current version: 3.7 - Date: Oct 2021

Named types

Enum

enum Size {
    S = 'small',
    L = 'large',
    XL = 'xlarge'
};

Interface

interface IPerson {
    firstname: string;
    age?: number; //optional
    sayHello(): void;
    size?: Size;
}

Class

class Person implement IPerson {
    firstname: string;
    age: number;
    size: Size = Size.L;

    constructor() {}

    sayHello(): void {};
}

Object type literals

Object with implicit properties

{ foo: 'hello'; bar: 5 } or { foo; 'hello'; }

Object with optional property

{ required: boolean; optional?: number; }

Hash map

{ [key: string]: Type; }

Indexable types

{ [id: number]: Type; }

Tuples

A tuple is a finite ordered list of elements. A tuple in TypeScript is much like a typed list except that it is immutable (unchangeable) once created.

type keyValuePair = [number, string];
let list: keyValuePair = [123, 'abc']

Union types

let myUnionVariable: number | string;

Classes

class CodeLock {
    id: number; // implicitly public
    public manufacturer: string;
    static brand = 'SecurityInc';
    constructor(id: number,
                manufacturer: string,
                private code?: string = "anon") {
        // [...]
    }
}
console.log(CodeLock.brand); // no instance
new CodeLock(1, "x", "secret").id; // ok
new CodeLock(1, "x", "secret").manufacturer; // ok
new CodeLock(1, "x", "secret").code; // warning

Generics

Function using type parameters

<T>(items:T[], callback:(item:T) => T):T[]

Interface with multiple types

interface Pair<T1, T2> {
  first:T1;
  second:T2;
}

Constrained type parameter

<T extends ConstrainedType>():T

Inheritance

validation.ts

export abstract class StringValidator {
    abstract isAcceptable(s: string): boolean;
}

let lettersRegexp = /^[A-Za-z]+$/,
    numberRegexp = /^[0-9]+$/;

export class Letters extends StringValidator {
    isAcceptable(s: string) {
        return lettersRegexp.test(s);
    }
}

Module import

random.ts

export default function random() {
    return Math.random();
}

hello-world.ts

import { StringValidator, Letters } from './validation'

import random from './hello-world'
import { random as RD } from './hello-world'

Elvis operator

Test existence of an element :

let x = foo?.bar;
// is equivalent to
let x = foo === null || foo === undefined ? undefined : foo.bar;