Language cheatsheet
TypeScript 7 Cheatsheet
Compiler commands, tsconfig options and the type patterns you use every day.
Compiler and running
- Install TypeScript — Add the compiler to a project as a development dependency.
- Check the compiler version — Print the installed compiler version.
- Create a tsconfig.json — Generate a tsconfig.json with commented options.
- Compile the project — Compile using tsconfig.json.
- Type-check without output — Check types and report errors, writing no files. The usual command in CI.
- Compile on changes — Recompile when files change.
- Use another config file — Compile with a specific tsconfig, for example one used only for builds.
- Show the resolved config — Print the final configuration, with extends and defaults applied.
- Run a TypeScript file with Node.js — Node.js 24 strips types and runs the file directly. It supports only syntax that erases to JavaScript, so enums and namespaces are not allowed by default.
- Run a TypeScript file with tsx — Run TypeScript with a runner that handles more syntax and module settings.
- Install type definitions — Add types for a library that doesn't ship its own.
tsconfig options
- A solid starting tsconfig — A strict configuration for a Node.js project using ES modules.
- Turn on strict mode — Enable the family of strict type-checking options. Recommended for new code.
- Treat indexed access as possibly undefined — Make array and object index access include undefined, catching missing-item bugs.
- Set up import aliases — Map an alias such as @/utils to a folder. Your bundler needs the same alias.
- Skip checking declaration files — Speed up type-checking by not checking .d.ts files from libraries.
- Keep imports as written — Make import type and export type explicit, which suits type stripping and bundlers.
Types
- Define an object type — Describe the shape of an object with an interface.
- Define a union type — Allow a value to be one of several types.
- Make a literal read-only — Infer the narrowest literal types and make properties readonly.
- Check a value without widening it — Verify a value matches a type while keeping its precise inferred type.
- Write a generic function — Keep the relationship between input and output types.
- Constrain a generic — Require that a type parameter has certain properties.
- Get the keys of a type — Build a union of an object type's keys.
- Get a type from a value — Derive a type from a runtime value so they can't drift apart.
- Get a function's return type — Extract the type a function returns; wrap it in Awaited for async functions.
Utility types and narrowing
- Make all properties optional — Create a type where every property of User is optional, handy for update payloads.
- Pick or omit properties — Create a type from a subset of another type's properties.
- Type a dictionary — Describe an object with known key and value types.
- Require or freeze properties — Make every property required, or read-only.
- Remove null and undefined — Exclude null and undefined from a type.
- Write a type guard — Tell the compiler how a check narrows a value.
- Narrow an unknown value — Check the type before using a value of type unknown, such as parsed JSON.
- Check a switch handles every case — Make the compiler fail if a union member is not handled.
Related cheatsheets
- C++ 23 cheatsheet — Compile and debug C++ with g++ and CMake, and use modern C++ (up to C++23): containers, algorithms, smart pointers, templates and threads.
- C# 14 cheatsheet — Modern C# 14 syntax for strings, nullability, collections, LINQ, records, pattern matching, async code and generics.
- CSS 3 cheatsheet — Layout with flexbox and grid, responsive design, custom properties, animations and modern selectors.
- Go 1 cheatsheet — The go command for modules, building, testing and cross-compiling, plus the code patterns you write every day.
- HTML 5 cheatsheet — HTML5 page structure, text, links, images, forms, tables and metadata, with accessible patterns.
- Java, Maven and Gradle 25 cheatsheet — JDK commands to compile, run and diagnose Java programs, plus the Maven and Gradle commands for builds and tests.