TypeScript Type Aliases

Creating custom type aliases

Basic Type Alias

type ID = string | number; # define type alias
let userId: ID = 123;

Object Type Alias

type User = { # object type
    name: string;
    age: number;
};
let user: User = {
    name: 'John',
    age: 25
};

Function Type Alias

type MathFunc = (x: number, y: number) => number; # function type
const add: MathFunc = (x, y) => x + y;

Intersection Type Alias

type Person = { name: string };
type Employee = { id: number };
type Staff = Person & Employee; # combine types

Type vs Interface

// Type: can use unions, primitives
// Interface: can be extended, merged