TypeScript Cheat Sheet
Last updated
Basic types
The primitive types and how to annotate variables.
| Operation | Syntax |
|---|---|
| String | let name: string = "Ada"; |
| Number | let age: number = 25; |
| Boolean | let ok: boolean = true; |
| Array of T | let xs: number[] = [1, 2, 3]; |
| Generic array | let xs: Array<string> = []; |
| Tuple | let pair: [string, number] = ["a", 1]; |
| Any (opt out of typing) | let x: any = 1; |
| Unknown (safer any) | let x: unknown = JSON.parse(s); |
| Void (no return) | function log(): void { … } |
| Never (never returns) | function fail(): never { throw … } |
| Null / undefined | let n: null = null; |
Type inference & annotations
When to let TS infer and when to annotate.
| Operation | Syntax |
|---|---|
| Inferred from value | let count = 0; // number |
| Explicit annotation | let count: number = 0; |
| Function parameter | function add(a: number, b: number) { … } |
| Return type | function add(a: number, b: number): number { … } |
| Arrow function | const sq = (x: number): number => x * x; |
| Type assertion | const el = input as HTMLInputElement; |
| Const assertion (literal) | const dirs = ["up", "down"] as const; |
| Non-null assertion | el!.focus(); |
Interfaces
Describe the shape of an object.
| Operation | Syntax |
|---|---|
| Declare | interface User { id: number; name: string; } |
| Optional property | interface User { age?: number; } |
| Readonly property | interface User { readonly id: number; } |
| Method signature | interface Logger { log(msg: string): void; } |
| Extend another interface | interface Admin extends User { role: string; } |
| Index signature | interface Map { [key: string]: number; } |
| Implement in a class | class Db implements Logger { … } |
Type aliases & unions
Name a type and combine types with | and &.
| Operation | Syntax |
|---|---|
| Type alias | type ID = string | number; |
| Object type alias | type User = { id: number; name: string }; |
| Union type | type Status = "idle" | "loading" | "done"; |
| Intersection type | type Admin = User & { role: string }; |
| Literal type | type Yes = "yes"; |
| Nullable | type Maybe<T> = T | null | undefined; |
| Function type alias | type Handler = (e: Event) => void; |
Functions
Typed parameters, returns, and overloads.
| Operation | Syntax |
|---|---|
| Optional parameter | function greet(name?: string) { … } |
| Default parameter | function greet(name = "friend") { … } |
| Rest parameters | function sum(...ns: number[]): number { … } |
| Function type | let fn: (a: number) => number; |
| Callable interface | interface Fn { (x: number): number; } |
| Overload signatures | function f(x: string): string; function f(x: number): number; function f(x: any) { return x; } |
| Void return ignored | const cb: () => void = () => 42; // ok |
Classes
Classes with access modifiers and parameter properties.
| Operation | Syntax |
|---|---|
| Declare a class | class User { name: string; } |
| Constructor | constructor(name: string) { this.name = name; } |
| Parameter property | constructor(public name: string) {} |
| Public / private / protected | private id: number; |
| Readonly field | readonly id: number; |
| Static member | static count = 0; |
| Getter / setter | get name() { return this._name; } |
| Inheritance | class Admin extends User { … } |
| Abstract class | abstract class Shape { abstract area(): number; } |
Generics
Reusable types parameterized by other types.
| Operation | Syntax |
|---|---|
| Generic function | function id<T>(x: T): T { return x; } |
| Call with explicit type | id<string>("hi"); |
| Generic interface | interface Box<T> { value: T; } |
| Generic type alias | type Pair<A, B> = [A, B]; |
| Generic class | class Stack<T> { items: T[] = []; } |
| Constraint with extends | function len<T extends { length: number }>(x: T) { return x.length; } |
| Default type parameter | interface Box<T = string> { value: T; } |
| keyof operator | type K = keyof User; // "id" | "name" |
| Index access type | type Name = User["name"]; |
Narrowing
Refine a union to a single type at runtime.
| Operation | Syntax |
|---|---|
| typeof check | if (typeof x === "string") { … } |
| instanceof check | if (e instanceof Error) { … } |
| in operator | if ("name" in obj) { … } |
| Equality narrowing | if (status === "done") { … } |
| Truthiness | if (value) { … } |
| Custom type guard | function isUser(x: any): x is User { return "id" in x; } |
| Discriminated union | type Shape = { kind: "sq"; size: number } | { kind: "ci"; r: number }; |
| Exhaustiveness with never | const _: never = shape; |
Enums
Named sets of constants.
| Operation | Syntax |
|---|---|
| Numeric enum | enum Direction { Up, Down, Left, Right } |
| String enum | enum Status { Idle = "IDLE", Done = "DONE" } |
| Use a value | let d: Direction = Direction.Up; |
| Const enum (inlined) | const enum Color { Red, Green, Blue } |
| Union of literals (modern) | type Status = "idle" | "done"; |
Utility types
Built-in helpers that transform existing types.
| Operation | Syntax |
|---|---|
| Make all props optional | Partial<User> |
| Make all props required | Required<User> |
| Make all props readonly | Readonly<User> |
| Pick a subset of keys | Pick<User, "id" | "name"> |
| Omit one or more keys | Omit<User, "password"> |
| Object with given keys | Record<string, number> |
| Exclude from a union | Exclude<"a" | "b" | "c", "a"> |
| Keep only from a union | Extract<"a" | "b", "a" | "c"> |
| Remove null / undefined | NonNullable<string | null> |
| Function return type | ReturnType<typeof fn> |
| Function parameter tuple | Parameters<typeof fn> |
| Awaited promise type | Awaited<Promise<string>> |
Modules
Import and export across files.
| Operation | Syntax |
|---|---|
| Named export | export function add(a: number, b: number) { … } |
| Default export | export default class User { … } |
| Re-export | export { User } from "./user"; |
| Named import | import { add } from "./math"; |
| Default import | import User from "./user"; |
| Import a type only | import type { User } from "./user"; |
| Import everything | import * as math from "./math"; |
| Triple-slash reference | /// <reference types="node" /> |
tsconfig essentials
The compiler flags worth knowing.
| Option | Effect |
|---|---|
"strict": true | Enables all strict-mode checks (recommended). |
"target": "ES2022" | JavaScript version tsc emits. |
"module": "ESNext" | Module system in emitted output. |
"moduleResolution": "bundler" | Resolve modules the way modern bundlers do. |
"jsx": "react-jsx" | Enable JSX for React 17+. |
"esModuleInterop": true | Cleaner default imports from CommonJS modules. |
"skipLibCheck": true | Skip type-checking node_modules (faster builds). |
"noEmit": true | Type-check only - leave emit to your bundler. |
The TypeScript syntax you reach for most, on one page. This TypeScript cheat sheet is a quick reference for everyday TS - basic types, interfaces, type aliases, generics, unions, narrowing, and the built-in utility types. Since TypeScript builds on JavaScript, keep the JavaScript cheat sheet handy for the underlying syntax.
Everything here is standard TypeScript that compiles with tsc and runs anywhere modern JavaScript does. Copy what you need, or try any snippet live in the TypeScript playground - nothing to install. New to types? Coddy's free interactive TypeScript course builds them up from JavaScript.
TypeScript cheat sheet FAQ
Is this TypeScript cheat sheet free?
What is the difference between an interface and a type alias?
interface. A common rule: use interface for public object shapes, and type for unions, tuples, and computed types.What is the difference between any and unknown?
any opts out of type checking entirely - the compiler will let you do anything to it. unknown is type-safe: you must narrow it (with typeof, instanceof, or a type guard) before you can use it. Prefer unknown for values from external sources like JSON.parse or fetch - it forces you to validate before using.