Skip to content

TypeScript Cheat Sheet — Complete Reference (55+ type patterns, 2026)

Last verified May 2026 — runs in your browser
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

TypeScript Cheat Sheet — Interfaces, Generics, Utility Types & Type Guards

TypeScript launched in 2012 as a typed superset of JavaScript; the type system has grown a lot since — generics, conditional types, `infer`, template literal types, the `satisfies` operator — while the runtime stays plain JavaScript with no checks at all. The cheatsheet below covers 55+ type patterns across basics, interfaces, generics, utility types, type guards, enums, and advanced features. Most trouble in real TypeScript code doesn't come from forgetting syntax. It comes from quirks that look ordinary but bite. `any` and `unknown` look similar but behave opposite: `any` opts out of checking, while `unknown` requires a narrowing check before the value can be used. The `as` cast tells the compiler to trust the developer and silently disables the check at that line. Structural typing means two types with the same shape are interchangeable, which surprises authors coming from Java or C#. The snippets below are the ones an author reaches for when debugging exactly that — the right narrowing predicate, the right utility type, the right constraint — without rereading the TypeScript reference from line one each time.

Common pitfalls in TypeScript

A few patterns earn their place on the first screen of any TypeScript file. Reach for `unknown` over `any` whenever a value's shape is uncertain — `unknown` keeps the type system on and forces a check (`typeof`, `instanceof`, or a custom predicate) before any property access. Prefer the `satisfies` operator over a type annotation when assigning a constant: it validates the shape while keeping the literal type narrow. Strict null checks turn `null` and `undefined` into distinct types that have to be excluded with optional chaining, the `??` operator, or a type guard; without the flag both values slip into every type silently. Type assertions with `as` should be a last resort — a custom predicate `x is User` is verifiable, while `x as User` is a promise the compiler accepts without proof. Index signatures `[key: string]: T` accept reads of keys that may not exist, returning `T | undefined` only when `--noUncheckedIndexedAccess` is enabled. The cheatsheet groups this into Basic Types, Interfaces, Generics, Utility Types, Type Guards, Enums, and Advanced so the right section is one click away.

  • 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

Free. No signup. Your inputs stay in your browser. Ads via Google AdSense (consent required).