Loading video player…

Reusable Types: Generics, Utility Types and Typed Modules (Overview)

This course is about writing a type once and deriving the rest.

The Riverside Kids’ Football League runs three volunteer scripts. Each one has its own copy of the league’s types, and one copy says the age group is a number. The type check passes anyway, since each script is checked only against its own copy:

Language: Shell
$ npm run check

> [email protected] check
> tsc --noEmit

But when you run the league table script, node league/league-table.ts, it finds none of the league’s two U10 teams:

Language: Text
U10 table: 0 teams

The league keeps its data behind a JSON API. In this course, you’ll write one typed client for it, a module that every script imports from. With your client, the same command finds both teams:

Language: Text
U10 table: 2 teams
Riverside Lions
Mill Lane Otters

When a type changes in the client, the checker points at every script you need to update.

What You’ll Learn

By the end of this course, you’ll be able to:

  • Type classes and modules
  • Keep to syntax that can be erased
  • Write generic functions and types that keep the relation between inputs and outputs
  • Derive types from one source with keyof, typeof and utility types

You’ll finish by building the league’s API client, then explaining it to the course’s AI mentor.

Before You Start

You should know type annotations, object types, type aliases, literal unions, type guards, ES modules, and JavaScript classes. You should also be able to read a generic type that someone else wrote, like Promise<T>.

You need a current Node.js release with long-term support (LTS) and an editor that knows TypeScript. This course’s code was run with this version:

Language: Shell
$ node --version
v26.13.1

How the Course Works

The course’s Sample Code (.zip), linked below this video, is the Riverside project: a strict TypeScript setup, a check script, npm run check, and a stand-in for the league’s API. You’ll set it up in the first lesson, and the project lesson adds the league’s three old scripts.

Coding exercises run in your browser, not in your local project. They sit beside text lessons, short videos, and a quiz. Next up is Typing a Class and Its Methods, on the class behind the league table script.

Download

Course Slides (.pdf)

961.0 KB
Download

Sample Code (.zip)

25.6 KB

00:00 Welcome to Reusable Types: Generics, Utility Types and Typed Modules. This course is about writing a type once and deriving the rest.

00:12 A kids’ football league’s three volunteer scripts each copy the league’s types. One copy says the age group is a number. The check passes, since each script is checked against its own copy.

00:24 But the league table finds no U10 teams, and there are two. By the end of this course, it finds both.

00:33 The Riverside Kids’ Football League keeps its data behind a JSON API. You’ll write one typed client for it, a module every script imports from. When a type changes there, the checker points at every script to update.

00:50 You’ll add types to classes and modules, keeping to syntax Node can strip away. Then you’ll write generic functions whose result type follows what you pass in.

01:01 You’ll derive types from one source with keyof, typeof and utility types. Last, you’ll build the league’s API client, then explain it to the AI mentor.

01:14 You should know type annotations, object types and aliases, and literal unions. You should also know type guards, modules and classes, and be able to read a generic type like a Promise.

01:27 You need a current Node release with long-term support and an editor that knows TypeScript.

01:34 The Sample Code, linked below, is the Riverside project: a strict TypeScript setup, a check script and a stand-in league API. You’ll set it up in the first lesson, and the project lesson adds the old scripts.

01:49 Coding exercises run in your browser, not in your local project. They sit beside text lessons, short videos and a quiz. Next up is Typing a Class and Its Methods, on the league table’s class.

Become a Member to join the conversation.