TypeScript Cheat Sheet

TypeScript type system quick reference. Basic types, interfaces, generics, utility types, type guards, and common patterns — all in one page.

Basic Types Interfaces & Types Generics Utility Types Type Guards & Narrowing Enums & Constants Common Patterns

Basic Types

let x: string = "hello" String type
let n: number = 42 Number type (int and float)
let b: boolean = true Boolean type
let arr: number[] = [1, 2] Array of numbers
let arr: Array<string> Array generic syntax
let t: [string, number] Tuple (fixed-length, typed)
let obj: { name: string; age: number } Object shape
let x: any Opt out of type checking (avoid)
let x: unknown Type-safe alternative to any
let x: void No return value (functions)
let x: never Never returns (throws or infinite loop)

Interfaces & Types

interface User { name: string; } Define object shape
interface Admin extends User { role: string; } Extend interface
type ID = string | number Union type (either)
type Point = { x: number; y: number } Type alias for object
type Status = "active" | "inactive" String literal union
interface Config { debug?: boolean } Optional property
interface Dict { [key: string]: number } Index signature
type Fn = (a: string) => boolean Function type

Generics

function id<T>(val: T): T { return val } Generic function
interface Box<T> { value: T } Generic interface
type Pair<A, B> = { first: A; second: B } Multiple type params
function fn<T extends HasId>(x: T) Constrained generic
function fn<T = string>(x: T) Default type parameter
type ValueOf<T> = T[keyof T] Extract value type from object

Utility Types

Partial<User> All properties optional
Required<User> All properties required
Readonly<User> All properties readonly
Pick<User, "name" | "email"> Select specific properties
Omit<User, "password"> Remove specific properties
Record<string, number> Object with typed keys and values
ReturnType<typeof fn> Extract function return type
Parameters<typeof fn> Extract function parameter types as tuple
Awaited<Promise<string>> Unwrap Promise type → string
NonNullable<string | null> Remove null and undefined
Extract<A | B | C, A | B> Keep only matching union members
Exclude<A | B | C, A> Remove matching union members

Type Guards & Narrowing

typeof x === "string" Narrow by primitive type
x instanceof Date Narrow by class instance
"name" in obj Narrow by property existence
Array.isArray(x) Narrow to array type
function isUser(x: any): x is User {} Custom type guard
x as string Type assertion (use sparingly)
x satisfies Config Validate type without widening (TS 4.9+)

Enums & Constants

enum Direction { Up, Down, Left, Right } Numeric enum (0, 1, 2, 3)
enum Status { Active = "active" } String enum
const Direction = { Up: 0 } as const Const object (preferred over enum)
type Dir = typeof Direction[keyof typeof Direction] Type from const object

Common Patterns

type Props = React.FC<{ title: string }> React functional component type
type Handler = React.ChangeEventHandler<HTMLInputElement> React event handler type
const obj = { a: 1 } as const Const assertion (literal types)
type Keys = keyof User Get union of object keys
type Val = User["name"] Indexed access type
declare module "lib" { ... } Declare external module types

Try It Live

Test these patterns with our free JSON to Types. No signup needed.

Open JSON to Types →

More Cheat Sheets