Loading video player…

Typing the Boundaries: Data From Outside Your Program (Overview)

TypeScript checks your code before it runs. Data from a file or an API arrives at run time, so the checker never sees it. In this course, you’ll check that data yourself at the boundary, where it enters your program.

The Millbrook Birders, a birdwatching club, log their sightings in a phone app that exports JSON. The club’s import script asks a species API about each bird and adds new ones to the year list, every species the club has seen this year. Its type check passes, but when it runs on May’s export, undefined goes on the year list where a bird’s name should be:

Language: Shell
$ node --env-file=.env imports.ts exports/may.json
Year list: 4 species
- Atlantic Puffin
- undefined
- Red Kite
- Little Egret

In this course, you’ll rewrite that import. On the same export, your version says why it turns sightings away and keeps undefined off the list:

Language: Shell
$ node --env-file=.env imports.ts exports/may.json
Rejected: Entry 2, species: Invalid input: expected string, received undefined
Rejected: Entry 4, species: Invalid input: expected string, received undefined
Not added: Unknown species: Eurasian Curlw
Year list: 3 species
- Atlantic Puffin
- Red Kite
- Little Egret

What You’ll Learn

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

  • Tell a type assertion that’s justified from one that hides a bug
  • Type async code
  • Treat data from files and APIs as unknown
  • Check it once at the boundary with type guards or a schema

You’ll finish by rewriting the club’s import, then explaining it to the course’s AI mentor.

Before You Start

You should know type annotations, union types and narrowing. You should also know why the type unknown is safer than the type any, and how to await a fetch() call.

You need a current Node.js release with long-term support (LTS) and an editor with TypeScript support. Node.js runs TypeScript files without checking their types. To see which version you have, run node --version:

Language: Shell
$ node --version
v26.13.1

How the Course Works

In the first lesson, you’ll download the course’s Sample Code (.zip): a Millbrook project with TypeScript, a strict setup, a check script, and a stand-in species API. The lessons build on it, and the project lesson adds the club’s old import.

The course has text lessons, short videos, and coding exercises in your browser, and it ends with a chat with the mentor, a quiz, and a summary. In the next lesson, Asserting a Type With as, you’ll see how that undefined got past the checker.

Download

Course Slides (.pdf)

689.1 KB
Download

Sample Code (.zip)

14.6 KB

00:00 Welcome to Typing the Boundaries: Data From Outside Your Program. TypeScript checks your code before it runs. Data from a file or an API arrives at run time, so the checker never sees it.

00:13 You’ll check it yourself at the boundary, where it enters.

00:18 Start with a birdwatching club’s import script. Its type check passes. Then it runs on May’s export, and undefined goes on the year list, where a bird’s name should be.

00:31 The Millbrook Birders log sightings in a phone app that exports JSON. The script asks a species API about each bird and adds new ones to the year list: every species seen this year.

00:43 You’ll rewrite it. Your version says why it turns sightings away, and keeps undefined off the list.

00:52 First, you’ll learn to tell a type assertion that’s justified from one that hides a bug. Then you’ll type async code. Next, you’ll treat outside data as unknown, and check it once at the boundary.

01:06 Finally, you’ll rewrite the import and explain it to an AI mentor.

01:12 You should know type annotations, union types and narrowing. You should also know why the type unknown is safer than the type any, and how to await a fetch call.

01:24 You need a current Node release with long-term support and an editor with TypeScript support. Node runs TypeScript files without checking their types.

01:34 The first lesson has you download the Sample Code: a Millbrook project with TypeScript, a strict setup, a check script and a stand-in species API. The lessons build on it, and the project lesson adds the old import.

01:49 The course has text lessons, short videos and browser coding exercises, and ends with a mentor chat, a quiz and a summary. In the next lesson, Asserting a Type With as, you’ll see how that undefined got past the checker.

Become a Member to join the conversation.