TypeScript: JavaScript with Types

Types, interfaces, unions and narrowing, classes, generics, utility, mapped and conditional types, typed async code and type-safe design, checked by the real TypeScript compiler.

Level
Intermediate
Time
About 22 hours
Modules
5
Lessons
15
Exercises
45

TypeScript is JavaScript with static types, and it is now the default language for large JavaScript projects, from VS Code to most popular libraries. This course teaches the type system from the first annotation to generics, mapped and conditional types, and shows how to design programs that the compiler can check.

Every example and exercise runs through the real TypeScript compiler, version 5.9, in strict mode, in your browser: a program with a type error is stopped with tsc's own message before it runs, and a program that passes runs as JavaScript with every step visible in the visualizer.

What you will learn

  • Annotate variables, functions and objects, and rely on inference where it is clear
  • Model data with interfaces, unions, literal types and discriminated unions
  • Narrow unions safely and make the compiler prove every case is handled
  • Write classes, abstract classes and interfaces with access modifiers
  • Write generic functions and classes, and use utility, mapped and conditional types
  • Type async code and design programs where invalid states cannot be written

Skills you will gain

  • Reading and fixing TypeScript compiler errors
  • Designing types for real data
  • Generic programming
  • Type-level programming with mapped and conditional types
  • Type-safe async code and error handling

Before you start

  • JavaScript: variables, functions, arrays, objects, classes and promises (the JavaScript course)
  • Comfort reading error messages and fixing a program step by step
  • Nothing to install: the TypeScript compiler runs in your browser

Where it is used

  • Front-end apps with React, Angular or Vue
  • Back-end services with Node.js
  • Libraries with typed public APIs
  • Moving an existing JavaScript codebase to TypeScript

Modules

Module 1: Types from the start

What TypeScript adds to JavaScript, the basic types, and typed functions with optional, default and rest parameters.

  1. 1

    Why TypeScript Intermediate

    What TypeScript adds to JavaScript, how the type checker finds mistakes before a program runs, type annotations and inference, and how a TypeScript program becomes JavaScript.

  2. 2

    Basic types: primitives, arrays, tuples and unknown Intermediate

    The primitive types, arrays and tuples, null and undefined under strict mode, literal types, and the difference between any and unknown.

  3. 3

    Typed functions Intermediate

    Parameter and return types, optional, default and rest parameters, function types for callbacks, void and never, and how TypeScript checks every call.

Module practice and quiz →

Module 2: Shaping data

Interfaces and object types, type aliases, unions and intersections, and narrowing a union down to one type.

  1. 1

    Object types and interfaces Intermediate

    Describing objects with interfaces and object types: optional and readonly properties, index signatures, nested shapes, structural typing and the check for extra properties.

  2. 2

    Type aliases, unions and intersections Intermediate

    Naming types with type aliases, combining them with unions (one of several) and intersections (all at once), unions of literal values, and discriminated unions.

  3. 3

    Narrowing: from a union to one type Intermediate

    How checks narrow a union to one of its members: typeof, equality, truthiness, instanceof, in, discriminants, exhaustive switches with never, and your own type guards.

Module practice and quiz →

Module 3: Classes and enums

Classes with typed fields and access modifiers, inheritance, abstract classes and implements, and enums and as const.

  1. 1

    Classes in TypeScript Intermediate

    Typed fields and constructors, parameter properties, public, private, protected and #private members, readonly fields, getters and setters, and static members.

  2. 2

    Inheritance, abstract classes and implements Intermediate

    Extending classes with extends and super, overriding methods, protected members, abstract classes that leave methods to subclasses, and classes that implement interfaces.

  3. 3

    Enums, as const and literal types Intermediate

    Numeric and string enums, what an enum becomes at run time, as const for read-only literal data, and deriving union types from constant arrays and objects with typeof and keyof.

Module practice and quiz →

Module 4: Generic code

Generic functions and constraints, generic classes and typed collections, and the built-in utility types.

  1. 1

    Generic functions Advanced

    Type parameters that let one function work with many types without losing type information: inference of type arguments, several type parameters, constraints with extends, and keyof constraints.

  2. 2

    Generic classes and typed collections Advanced

    Generic classes and interfaces, default type parameters, the typed collections Map, Set and ReadonlyArray, and building your own data structures that work for any item type.

  3. 3

    Utility types Advanced

    The built-in types that transform other types: Partial, Required, Readonly, Pick, Omit, Record, Exclude, Extract, NonNullable, ReturnType and Parameters, and when to use each.

Module practice and quiz →

Module 5: Advanced types and real programs

Mapped, conditional and template literal types, typed async code, and designing programs where invalid states cannot be written.

  1. 1

    Mapped, conditional and template literal types Advanced

    Types that compute other types: keyof and indexed access, mapped types over an object's keys, key remapping, conditional types with infer, and template literal types.

  2. 2

    Async code with types Advanced

    Promise<T> and async functions, typing results of Promise.all and Promise.allSettled, errors in catch as unknown, and modelling success and failure with a Result type.

  3. 3

    Designing type-safe programs Advanced

    Putting the course together: parse at the edges and trust types inside, make invalid states unrepresentable with discriminated unions, branded types for values that look alike, readonly data, Result types and exhaustive checks.

Module practice and quiz →

Projects

Final assessment →

What to learn next