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

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

You’ve finished Reusable Types: Generics, Utility Types and Typed Modules. You started with three volunteer scripts for the Riverside Kids’ Football League, each with its own copy of the league’s types, and the copies disagreed. You finished with one typed client that holds every type once and derives the rest.

What You Can Do Now

  • 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

How You Got There

  • The results table became a class checked against an interface: class ResultsTable implements TableSource.
  • The coach’s phone moved into a #coachPhone field, which JavaScript keeps private at run time, where TypeScript’s private is erased.
  • An enum gave way to an as const object and a literal union, which Node can strip.
  • A function type, CompareRows, caught a comparator that returned a boolean where sorting needs a number.
  • The league’s types got one module, imported with import type.
  • A generic request kept the caller’s type, and one result type, ApiResult<T>, wrapped every reply.
  • The age groups became one as const list that the AgeGroup type comes from, with (typeof AGE_GROUPS)[number].
  • Utility types derived the create and update types: NewTeam is Omit<Team, "id">, and TeamUpdate builds on Partial.

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 finished Reusable Types: Generics, Utility Types and Typed Modules. Here’s what you can do now. You can type classes and modules. You can keep to syntax that can be erased.

00:13 You can write generic functions and types that keep the relation between inputs and outputs. And you can derive types from one source with keyof, typeof and utility types.

00:25 You started with three volunteer scripts, each with its own copy of the league’s types. The copies disagreed.

00:33 You typed the results table against an interface. You moved the coach’s phone into a JavaScript private field, which stays hidden at run time. You swapped an enum for an object and a union.

00:45 A function type caught a comparator that returned a boolean. You gave the types one module.

00:53 A generic request kept the caller’s type, and one result type wrapped every reply. The age groups became a list their type comes from. Utility types derived the create and update types.

01:08 Then the league got its own module, the client. Every script imports its types, and the functions it calls, from it. NewPlayer now came from Player. The checker said both sign-ups were missing a position and a guardian.

01:24 The old copy had hidden what was missing.

01:28 When Node runs the client, the types are gone. The client’s functions and class stay. Of these three lines, only the age group list is left. Remember these.

01:41 Write a type once, and derive the rest. A type parameter links what goes in to what comes out. Use import type when an import brings in only types. Read the error from the top before you fix it.

01:57 The checker checks the types on every line, including an AI assistant’s. It can’t check what the code does. To review that, read the RealJS tutorial The JavaScript You Need to Review AI-Written Code.

02:12 Thanks for building the league’s client.

Become a Member to join the conversation.