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

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

In this course, you rewrote the Millbrook Birders’ import. The old script asserted that the export held sightings, the type check passed, and undefined went on the year list. The new one checks every value from outside before it trusts its type.

What You Can Do Now

After working through this course, you can:

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

Things to Remember

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 You’ve rewritten the Millbrook Birders’ import. Now you can treat data from files and APIs as unknown, and check it once at the boundary with type guards or a schema.

00:11 You can type async code, and tell a type assertion that’s justified from one that hides a bug.

00:18 It started with an assertion in the old import. The type check passed, and undefined went on the year list.

00:25 An entry comes in as unknown. When the type guard isSighting returns true, it narrows to a sighting. An async function returns a promise of its value, and await gives you the value back.

00:41 The import has three boundaries: the export, the API’s reply, and the environment variable. The export and the reply get the type unknown. The environment variable keeps its honest type, and the API’s address in it is checked before any lookup.

00:57 A schema checks each entry, and the Sighting type comes from it. On May’s export, the finished import says why it turns sightings away, and undefined stays off the year list.

01:12 The checker reads your code before it runs. Then the types are erased, so outside data arrives unchecked. Give it the type unknown where it enters, or its source’s honest type.

01:24 Check it once there, with a type guard or a schema. Save a type assertion for what something else already guarantees.

01:33 Next on the TypeScript for JavaScript Developers path is a course called Reusable Types: Generics, Utility Types and Typed Modules. For a wider view, there’s the RealJS article JavaScript vs TypeScript: When to Add Types.

01:49 Thanks for working through this course with the Millbrook Birders.

Become a Member to join the conversation.