TypeScript Cheat Sheet

Core TypeScript syntax, typing patterns, and compiler commands.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Setup and CLI
Initialize tsconfig
npx tsc --init

# Create a starter tsconfig.json in the current project.

Show TypeScript version
npx tsc --version

# Print the installed TypeScript compiler version.

Type-check without emitting JS
npx tsc --noEmit

# Run the compiler only for type-checking.

Compile in watch mode
npx tsc --watch

# Rebuild automatically when files change.

Compile a specific project file
npx tsc -p tsconfig.json

# Use a given tsconfig path.

Build project references
npx tsc --build

# Use build mode for multi-project repos.

Explain included files
npx tsc --explainFiles

# Show why files are part of the compilation.

Trace module resolution
npx tsc --traceResolution

# Print how imports are resolved.

## Primitive Types and Annotations
Annotate basic variables
const appName: string = "CheatSheet";
const port: number = 3000;
const isProd: boolean = false;

# Explicitly type strings, numbers, and booleans.

Type arrays
const ids: number[] = [1, 2, 3];
const names: Array<string> = ["Ada", "Linus"];

# Declare arrays using `T[]` or `Array<T>`.

Annotate object shapes
const user: { id: number; name: string; admin?: boolean } = {
  id: 1,
  name: "Ada"
};

# Describe the expected properties of an object.

Use a union type
let value: string | number;
value = "ok";
value = 42;

# Allow multiple possible primitive types.

Restrict values with literal unions
type Theme = "light" | "dark" | "system";
const theme: Theme = "dark";

# Use exact string values for safer APIs.

Rely on inference
const count = 10;      // number
const title = "Hello"; // string

# Let TypeScript infer common variable types.

Freeze literals with `as const`
const statusMap = {
  ok: 200,
  notFound: 404,
} as const;

# Preserve readonly literal values for objects and arrays.

## Objects, Interfaces, and Type Aliases
Define an interface
interface User {
  id: number;
  name: string;
  email?: string;
}

# Use an interface to model a reusable object shape.

Define a type alias
type UserId = string | number;
type Point = { x: number; y: number };

# Use a type alias for unions, objects, tuples, or helpers.

Extend an interface
interface Person {
  name: string;
}

interface Employee extends Person {
  team: string;
}

# Compose interfaces with inheritance.

Combine shapes with intersection
type Audit = { createdAt: Date };
type Customer = { id: string; name: string };

type AuditedCustomer = Customer & Audit;

# Merge multiple types into one.

Mark properties readonly
type Config = {
  readonly apiBaseUrl: string;
  readonly retries: number;
};

# Prevent mutation through the type system.

Use an index signature
type StringMap = {
  [key: string]: string;
};

# Type dictionary-like objects with dynamic keys.

## Tuples and Enums
Declare a tuple
const point: [number, number] = [10, 20];

# Model a fixed-size array with known positions.

Use named tuple members
type HttpResponse = [status: number, body: string];

# Improve tuple readability with labels.

Create a numeric enum
enum Direction {
  Up,
  Down,
  Left,
  Right,
}

# Declare a group of related named constants.

Use a string enum
enum Status {
  Draft = "draft",
  Published = "published",
}

# Give enum members stable string values.

Recommended next

No recommendations yet.