TypeScript Cheatsheet

TypeScript Cheatsheet
let x: number = 10

Number type

Basic Types
let s: string = "hello"

String type

Basic Types
let b: boolean = true

Boolean type

Basic Types
let arr: number[] = [1, 2, 3]

Array type

Basic Types
let tuple: [string, number]

Tuple type (fixed length & types)

Basic Types
let x: any

Any type (opt out of type checking)

Basic Types
let x: unknown

Unknown type (safer than any)

Basic Types
let x: never

Never type (unreachable code)

Basic Types
let x: void

Void type (no return value)

Basic Types
let x: null | undefined

Null and undefined types

Basic Types
type ID = string | number

Union type

Basic Types
type Point = { x: number; y: number }

Type alias for object

Basic Types
const x = "hello" as const

Const assertion (literal type)

Basic Types
interface User { name: string; age: number; }

Interface declaration

Interfaces
interface User { email?: string; }

Optional property

Interfaces
interface User { readonly id: number; }

Readonly property

Interfaces
interface Admin extends User { role: string; }

Interface inheritance

Interfaces
interface Fn { (x: number): string; }

Callable interface

Interfaces
interface Dict { [key: string]: number; }

Index signature

Interfaces
interface A extends B, C {}

Multiple inheritance

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

Generic function

Generics
interface Box<T> { value: T; }

Generic interface

Generics
class Container<T> { constructor(public value: T) {} }

Generic class

Generics
function fn<T extends HasLength>(x: T)

Generic constraint

Generics
function fn<T, K extends keyof T>(obj: T, key: K)

keyof constraint

Generics
type Pair<T, U = T> = [T, U]

Generic with default type

Generics
function fn<T extends string | number>(x: T)

Union constraint

Generics
Partial<User>

Make all properties optional

Utility Types
Required<User>

Make all properties required

Utility Types
Readonly<User>

Make all properties readonly

Utility Types
Pick<User, "name" | "age">

Pick subset of properties

Utility Types
Omit<User, "password">

Omit specific properties

Utility Types
Record<string, number>

Object type with key/value types

Utility Types
ReturnType<typeof fn>

Extract function return type

Utility Types
Parameters<typeof fn>

Extract function parameter types

Utility Types
Awaited<Promise<string>>

Unwrap Promise type

Utility Types
NonNullable<string | null>

Remove null/undefined from type

Utility Types
Extract<A | B | C, A | B>

Extract matching union members

Utility Types
Exclude<A | B | C, A>

Remove matching union members

Utility Types
if (typeof x === "string")

typeof guard

Type Guards
if (x instanceof Date)

instanceof guard

Type Guards
if ("name" in obj)

in operator guard

Type Guards
function isUser(x: any): x is User { return "name" in x; }

Custom type predicate

Type Guards
x as string

Type assertion

Type Guards
x satisfies Type

Satisfies operator (TS 4.9+)

Type Guards
enum Direction { Up, Down, Left, Right }

Numeric enum (0, 1, 2, 3)

Enums
enum Status { Active = "ACTIVE", Inactive = "INACTIVE" }

String enum

Enums
const enum Color { Red, Green, Blue }

Const enum (inlined at compile)

Enums
type Keys = keyof User

Get union of object keys

Advanced
type Val = User["name"]

Indexed access type

Advanced
type Mapped = { [K in keyof T]: boolean }

Mapped type

Advanced
type IsString<T> = T extends string ? "yes" : "no"

Conditional type

Advanced
type Flatten<T> = T extends Array<infer U> ? U : T

infer keyword

Advanced
type Literal = "a" | "b" | "c"

String literal union type

Advanced
declare module "lib" { export function fn(): void; }

Module declaration (ambient)

Advanced
// @ts-ignore

Suppress next-line TS error

Advanced
// @ts-expect-error

Expect error on next line

Advanced
Showing 57 of 57 entries

Free Online TypeScript Cheatsheet

A searchable TypeScript type system reference. Covers basic types, interfaces, generics, utility types, type guards, enums, and advanced patterns like conditional types and mapped types.

About this cheatsheet

A comprehensive TypeScript cheatsheet focused on the type system and patterns.

  • 55+ TypeScript type patterns
  • 7 categories from basics to advanced
  • All utility types covered
  • Generic patterns and constraints
  • One-click copy to clipboard
  • Covers TypeScript 5.x features

100% free. No signup required. No data collected or sent anywhere.

Explore All Tools