TypeScript, by example
TypeScript by example - annotated code snippets covering the language from first principles to production.
15 lessons · Updated 2026-04-23
- 01Hello, WorldRunning TypeScript with tsx and compiling with tsc - the two modes every TypeScript project uses.
- 02Basic TypesTypeScript's built-in scalar types - string, number, boolean, null, undefined, symbol, bigint, any, unknown, never.
- 03Type InferenceHow TypeScript infers types from assignments - const vs let narrowing, as const, and return type inference.
- 04InterfacesDeclaring interfaces - optional and readonly properties, extending interfaces, and declaration merging.
- 05Type AliasesType aliases vs interfaces, recursive types, template literal types, and discriminated unions.
- 06Union TypesUnion (|), intersection (&), literal unions, and discriminated unions - the building blocks of TypeScript's variant-safe modeling.
- 07GenericsType parameters, generic functions and interfaces, constraints with extends, and default type parameters.
- 08FunctionsTyped function signatures, optional and rest parameters, function type expressions, and overloads.
- 09ClassesConstructor shorthand, access modifiers, readonly, abstract classes, implementing interfaces, and ECMAScript private fields.
- 10EnumsNumeric, string, and const enums; heterogeneous enums; reverse mapping; and when to use string literal unions instead.
- 11Mapped TypesBuild new types by transforming every key of an existing type - the mechanism behind Partial, Readonly, and the rest of the TypeScript stdlib.
- 12Utility TypesThe TypeScript stdlib ships 20+ built-in utility types - Partial, Readonly, Pick, Omit, Exclude, ReturnType, and Awaited cover most day-to-day needs.
- 13NarrowingControl-flow analysis, type guards, assertion functions, and exhaustive checks - how TypeScript proves a value's type at each point in the program.
- 14ModulesES module import/export in TypeScript, import type, barrel re-exports, path aliases, moduleResolution strategies, and isolatedModules.
- 15Declaration FilesWriting .d.ts files, augmenting global types, @types/ version skew, and generating declarations from OpenAPI or GraphQL schemas instead of handwriting them.
Enjoyed this? Get more essays on software craft delivered to your inbox.
Subscribe free