TypeScript tutorial

Learn how TypeScript makes JavaScript safer and easier to maintain.

Start the course

Course contents

TypeScript Foundations

  1. TypeScript Basics — What TypeScript is, what its type checker catches and what it cannot, how compiling erases types, and how to install it and compile your first file.
  2. Variables and Types — Creating variables with let and const, naming and scope rules, the basic types from number to void, and how to convert a value from one type to another.
  3. Annotations and Inference — Where to write a type yourself, where TypeScript works it out for you, and how a callback gets its type for free from the code around it.

Types and Structures

  1. Collections — Typed arrays, the methods you use every day, and tuples for a fixed group of values whose types differ by position.
  2. Objects — Object types from required and optional properties to nested shapes, plus structural typing, excess property checking and index signatures.
  3. Interfaces — Naming an object shape with an interface: required, optional and readonly properties, methods, and building one interface on another with extends.
  4. Aliases — Naming any type with a type alias, what only an alias can express, and how to choose between an alias and an interface.
  5. Combination Types — Unions and intersections, literal types and literal unions, why an exact value widens, what as const does, and why a literal union usually beats an enum.
  6. Narrowing — How ordinary checks tell TypeScript which member of a union you hold: typeof, equality, in, instanceof and Array.isArray, then discriminated unions with exhaustive switches.
  7. Functions — Declarations, expressions and arrow functions; types on parameters and return values; void and never; optional, default and rest parameters; and typing a callback.

Object-Oriented TypeScript

  1. Classes — Building objects from a blueprint: properties, constructors, parameter properties, methods, public/private/protected, readonly and static members, getters and setters, encapsulation, and implements.
  2. Inheritance — Building one class on another with extends and super, replacing methods with override, protected members, inheritance versus composition, and abstract classes.

Application Integration

  1. Modules — Splitting a program across files: named and default exports, importing types, re-exports and barrels, import paths, import type, and avoiding circular dependencies.
  2. Configuring — What tsconfig.json does and which settings matter: strict and its sub-options, target, module, moduleResolution, rootDir, outDir, include, exclude and source maps.
  3. APIs — Typing data that arrives from a server: response models, success-or-failure types, the any holes left by fetch and JSON.parse, checking data while the program runs, and narrowing caught errors.

Advanced Type System

  1. Generics — Writing one function or type that works for many types without giving up checking: type parameters, generic arrays and tuples, inference, and explicit type arguments.
  2. Constraints — Generic interfaces and classes, limiting a placeholder with extends, and the keyof plus K extends keyof T pattern behind every safe property reader.
  3. Assertions — Telling the compiler a type with as, the non-null and definite assignment assertions, what each one hides, and the checked alternative to every one of them.
  4. Utilities — Building new types from ones you already have: Partial, Required, Readonly, Record, Pick and Omit, and how to combine two of them.

Reference

  1. Cheat Sheet — Every form taught in this course on one page, grouped by topic, with a runnable example for each and a pointer to the page that explains it.
  2. Compiler Errors — An index of the TypeScript error messages you will actually meet, each with its cause, a runnable example and the fix that solves it rather than silencing it.