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

Project setup, compiler usage, and quick inspection.

Initialize tsconfig

Create a starter tsconfig.json in the current project.

bashANYtypescripttsctsconfigsetup
bash
npx tsc --init

Generates a starter `tsconfig.json` file with common compiler options.

Show TypeScript version

Print the installed TypeScript compiler version.

bashANYtypescripttscversion
bash
npx tsc --version

Useful when debugging behavior differences across TypeScript releases.

Type-check without emitting JS

Run the compiler only for type-checking.

bashANYtypescripttsctypecheckci
bash
npx tsc --noEmit

Common CI command for catching errors without writing build output.

Compile in watch mode

Rebuild automatically when files change.

bashANYtypescripttscwatch
bash
npx tsc --watch

Keeps the compiler running and recompiles on file changes.

Compile a specific project file

Use a given tsconfig path.

bashANYtypescripttsctsconfigproject
bash
npx tsc -p tsconfig.json

Pass `-p` or `--project` to point TypeScript at a specific config file or directory.

Build project references

Use build mode for multi-project repos.

bashANYtypescripttscbuildproject-references
bash
npx tsc --build

Build mode is intended for composite projects and project references.

Explain included files

Show why files are part of the compilation.

bashANYtypescripttscdebuggingfiles
bash
npx tsc --explainFiles

Useful for debugging include/exclude and file graph issues.

Trace module resolution

Print how imports are resolved.

bashANYtypescripttscmodule-resolutiondebugging
bash
npx tsc --traceResolution

Useful when path aliases or package resolution behave unexpectedly.

Primitive Types and Annotations

Basic types, annotations, inference, and literal behavior.

Annotate basic variables

Explicitly type strings, numbers, and booleans.

typescriptANYtypescriptprimitivesannotations
typescript
const appName: string = "CheatSheet";
const port: number = 3000;
const isProd: boolean = false;

Type annotations are optional in many cases because TypeScript can infer them.

Type arrays

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

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

Both array syntaxes are common; choose one style and stay consistent.

Annotate object shapes

Describe the expected properties of an object.

typescriptANYtypescriptobjectsannotations
typescript
const user: { id: number; name: string; admin?: boolean } = {
  id: 1,
  name: "Ada"
};

Optional properties use `?` in object type annotations.

Use a union type

Allow multiple possible primitive types.

typescriptANYtypescriptunions
typescript
let value: string | number;
value = "ok";
value = 42;

Union types are one of the most common TypeScript patterns.

Restrict values with literal unions

Use exact string values for safer APIs.

typescriptANYtypescriptliteral-typesunions
typescript
type Theme = "light" | "dark" | "system";
const theme: Theme = "dark";

Literal unions are ideal for config values, modes, and command flags.

Rely on inference

Let TypeScript infer common variable types.

typescriptANYtypescriptinference
typescript
const count = 10;      // number
const title = "Hello"; // string

Inference usually produces cleaner code than over-annotating every value.

Freeze literals with `as const`

Preserve readonly literal values for objects and arrays.

typescriptANYtypescriptas-constliteral-types
typescript
const statusMap = {
  ok: 200,
  notFound: 404,
} as const;

`as const` narrows values to their literal types and makes properties readonly.

Objects, Interfaces, and Type Aliases

Reusable object shapes and composition patterns.

Define an interface

Use an interface to model a reusable object shape.

typescriptANYtypescriptinterfaceobjects
typescript
interface User {
  id: number;
  name: string;
  email?: string;
}

Interfaces are commonly used for object shapes, especially public APIs.

Define a type alias

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

typescriptANYtypescripttype-alias
typescript
type UserId = string | number;
type Point = { x: number; y: number };

Type aliases are more flexible than interfaces for unions and mapped types.

Extend an interface

Compose interfaces with inheritance.

typescriptANYtypescriptinterfaceextends
typescript
interface Person {
  name: string;
}

interface Employee extends Person {
  team: string;
}

`extends` helps model shared fields without duplication.

Combine shapes with intersection

Merge multiple types into one.

typescriptANYtypescriptintersection-types
typescript
type Audit = { createdAt: Date };
type Customer = { id: string; name: string };

type AuditedCustomer = Customer & Audit;

Intersection types are useful when enriching objects with shared metadata.

Mark properties readonly

Prevent mutation through the type system.

typescriptANYtypescriptreadonly
typescript
type Config = {
  readonly apiBaseUrl: string;
  readonly retries: number;
};

Readonly protects values from reassignment after initialization.

Use an index signature

Type dictionary-like objects with dynamic keys.

typescriptANYtypescriptindex-signatureobjects
typescript
type StringMap = {
  [key: string]: string;
};

Index signatures are useful for config maps and lookup tables.

Tuples and Enums

Position-based arrays and enum patterns.

Declare a tuple

Model a fixed-size array with known positions.

typescriptANYtypescripttuples
typescript
const point: [number, number] = [10, 20];

Tuples are useful when position matters and object keys would be excessive.

Use named tuple members

Improve tuple readability with labels.

typescriptANYtypescripttuples
typescript
type HttpResponse = [status: number, body: string];

Tuple labels improve editor help without changing runtime behavior.

Create a numeric enum

Declare a group of related named constants.

typescriptANYtypescriptenum
typescript
enum Direction {
  Up,
  Down,
  Left,
  Right,
}

Enums exist in TypeScript, though many teams prefer literal unions for simpler output.

Use a string enum

Give enum members stable string values.

typescriptANYtypescriptenumstring-enum
typescript
enum Status {
  Draft = "draft",
  Published = "published",
}

String enums are easier to debug than numeric enums in serialized output.

Recommended next

No recommendations yet.