Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Finding the Program's Boundaries

In this lesson, you watched an annotation let the renamed-field export through to the year list, saw why, and found the three places where data enters the club’s import: the export, the species API’s reply, and an environment variable for the API’s address.

Key Points

  • An annotation on outside data checks nothing. The old script asserted the export’s type with as. An annotation looks like the safer replacement, since one caught the typo in the settings. But with the species field renamed to commonName, const sightings: Sighting[] = JSON.parse(text); passes npm run check with no errors, and the loop puts undefined on the year list.
  • JSON.parse() returns any. Its hover ends in : any, and any switches checking off, so the checker accepts the result as an array of sightings, or as whatever type you write.
  • The types are gone when the data arrives. Node.js strips the types before it runs the file, so line 4 runs as const sightings = JSON.parse(text);. The checker reads your code before it runs, and the export’s data arrives only while the program runs, with no type left to compare it with.
  • unknown keeps the checker on guard. With const sightings: unknown = JSON.parse(text);, the check reports error TS18046, “‘sightings’ is of type ‘unknown’”, at the loop. That’s the error you want, because the loop can’t pass the check until something checks the export. unknown is erased too, so Node still runs the file and prints Year list: undefined.
  • A boundary is a place where data comes in from outside the program. To spot one, ask whether a value comes from something your code didn’t write, such as a file, the network or the environment. A value that arrives as any is often the sign. The import has three:
    • The export. Its text arrives as a plain string, and JSON.parse() is where it turns into data of any shape, so that’s where it gets unknown.
    • The species API’s reply. response.json() returns Promise<any>, so the awaited reply has type any, like the value JSON.parse() returns. You typed it as unknown in the first lesson, because that’s where the reply enters.
    • The API’s address, read from an environment variable with process.env.SPECIES_API. Its type is string | undefined, an honest type. Node gives an environment variable as text or leaves it unset, and the type claims no more than that. It says nothing about what the text holds, so the check has to confirm that it’s there and that it’s a URL.
  • Reading process.env needs Node’s types. process belongs to Node.js, not to JavaScript, so the check reports error TS2591, “Cannot find name ‘process’”, until you take both steps the message names. Install @types/node as a dev dependency, then add "types": ["node"] to compilerOptions in tsconfig.json, because current TypeScript doesn’t pick up installed type packages on its own.

Final Code

Install Node’s types in the millbrook-birders folder:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Last spring, an app update renamed the species field in the members’ export. The old script’s type assertion, with as, let it through, and undefined went on the year list.

00:11 In this lesson, you’ll find where data enters the import, and give it a type that doesn’t trust it.

00:18 Here’s Grace Liu’s export, cut down to one sighting. The species field is now called commonName. Without the assertion, you’d annotate what JSON.parse() returns as an array of sightings.

00:30 An annotation caught the typo in the settings, so it looks safer. Then the loop puts each species on the year list. Your turn: pause the video and predict.

00:41 Does this pass the type check?

00:47 It does: npm run check reports no errors. Run the file, and the year list gets undefined, the same bug as last spring.

00:59 Hover over parse: it returns any, whatever the text is. The any type switches checking off, so the checker accepts it as an array of sightings, or any type you write.

01:12 Node strips the types before it runs the file. On line 4, the annotation is gone. The checker read your code before it ran. The export’s data arrives only while the program runs, with no type left to compare it with.

01:26 So an annotation on data from outside is a claim that nothing checks.

01:33 So change the annotation to unknown instead. Unlike any, unknown lets you use a value only after a check narrows it. Check again: error TS18046 at line 6. Sightings is of type unknown, so the loop can’t pass the check until your code tests the export.

01:54 Unknown is erased too, so Node still runs it. The failing check is your warning.

02:00 The export arrives as plain text. JSON.parse() turns it into data of any shape, so that’s where it enters the import. It’s a boundary, a place where data comes in from outside the program.

02:12 The import has two more.

02:16 The second boundary is the species API’s reply. Here’s a lookup for the Red Kite. Hover over json: it returns a Promise of any, so the awaited reply is any.

02:28 You typed this reply as unknown in the first lesson, because it’s where the reply enters.

02:35 The third boundary is the API’s address. It’s written into the script now, but the club wants to change it without editing code, so it comes from an environment variable, in process.env.

02:47 Check again. Error TS2591 is new: the checker can’t find the name process. Process belongs to Node, not to JavaScript, so its types come from a separate npm package.

03:01 The message names two steps: install @types/node, then add node to the types field in your tsconfig.

03:10 Take the first step: install it as a dev dependency. Check again, and the same error is still there. Current TypeScript doesn’t pick up installed type packages on its own.

03:21 So take the second step: in tsconfig.json, add a types list with node to the compiler options. Check again, and the error on process is gone.

03:33 Hover over API: it’s a union of string and undefined, an honest type. Node gives an environment variable as text, or leaves it unset, and the type claims no more.

03:45 It says nothing about what the text holds, so the check is for two things: that it’s there, and that it’s a URL. Line 11 uses it unchecked, and the checker doesn’t object.

03:57 The check you’ll write next covers it.

04:01 The import has three boundaries: the export, the API’s reply, and the environment variable. To spot one, ask whether a value comes from something your code didn’t write.

04:12 Arriving as any is often the sign. Your types are erased before outside data arrives, so an annotation there checks nothing. Give the data the type unknown where it enters, or the honest type its source already has.

04:27 You’ll write each boundary’s check in Checking Data Once at the Boundary.

Become a Member to join the conversation.