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

Reading Generic Types

In this lesson, you read the angle brackets in the types of the club’s per-species counts, its year list and an async function, and wrote them yourself for a map and a set that start empty.

Key Points

  • A generic type names a kind of value, then what it holds. The club’s counts are a Map, and the checker shows Map<string, number>. Map is the kind of value. The types inside the angle brackets are type arguments: string is the type of every key, and number is the type of every value. Like a function’s arguments, they fill in the details, but they’re types, and only the checker uses them.
  • The type arguments flow into the methods. counts.get("Red Kite") is number | undefined. The .get() method takes a key of the key type, string, and returns the value type, number, or undefined, because a map may not hold the key you ask for.
  • A set takes one type argument. The year list is a Set<string>. A Set holds values without keys, so its one type argument is the type of each item.
  • Read nested types from the outside in. newSpecies() is an async function, and its return type is Promise<string[]>. The outer layer is a promise. If it fulfills, it gives you a string[], an array of strings that holds the species names. Array<string> is the same type as string[], spelled as a generic. This course writes the square brackets.
  • Fill in the type arguments when a collection starts empty. With nothing to infer from, new Map() is Map<any, any>, so nothing you put in it or read from it gets checked. An empty new Set() is Set<unknown>, which accepts any value you add. npm run check stays clean all the same, even in strict mode, so nothing warns you. Write new Map<string, number>() and new Set<string>(). When you create a collection with values, let the checker infer its types.
  • Type arguments are erased with the other types. Node.js creates an ordinary map and set, and fills them as usual.

Final Code

Here’s yearlist.ts at the end of the lesson. counts and yearList start empty with their type arguments written, and newSpecies() is a cut-down version of a lookup that will wait on the species API. The file is a toy for reading types. kites and newSpecies() stay for their hovers, and the run doesn’t use them:

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 Next, the import looks species up through the club’s API, and that code is async. Its types have angle brackets in them. In this lesson, you’ll learn to read those brackets, and when to write them yourself.

00:13 Here are the club’s counts per species, in a map. Hover over counts, and it’s a Map of string and number, in the checker’s words. Get the red kites’ count, and the result is number or undefined, because a map may not hold the key you ask for.

00:31 Read that type in two parts. Map, before the brackets, is the kind of value. The types inside the brackets say what this map holds. The first, string, is the type of every key.

00:45 The second, number, is the type of every value. These are type arguments. Like a function’s arguments, they fill in the details. Unlike them, they’re types, and only the checker uses them.

00:59 That’s where number or undefined came from. The get method takes a key of the key type, string. It returns the value type, number, or undefined. A type that takes type arguments like this is a generic type.

01:15 The year list is a set of species names. Hover over it, and it’s a Set of string.

01:22 A set holds values without keys, so its one type argument is the type of each item.

01:30 Now an async function, newSpecies. It takes a day’s species names and keeps the ones that aren’t on the year list yet. It’s a cut-down version. The real one will wait on the species API.

01:42 Hover over it, and its return type is a Promise of string array. Read it from the outside in. The outer layer is a promise, which an async function always returns.

01:54 Its type argument says what the promise gives you if it fulfills: a string array. And the array holds strings, the species names. You’ll also see a string array written as Array of string.

02:07 It’s the same type, spelled as a generic. This course writes the square brackets.

02:14 The real import starts with no counts at all. It starts with an empty map and an empty set, and fills them as sightings come in. With nothing to infer from, the checker can’t tell what they’ll hold.

02:26 Hover over counts. It’s a Map of any and any. So kites is any, and nothing you put in this map or read from it gets checked. The empty set comes out differently, as a Set of unknown, so the checker lets you add a number to it.

02:41 And npm run check stays clean, even in strict mode. So fill in the type arguments yourself. Put string and number in angle brackets after new Map. After new Set, put string in the brackets.

02:56 Hover again. Now counts is a Map of string and number. A collection created with values doesn’t need this, so let the checker infer its types.

03:08 This file is a toy for reading types, so the run doesn’t use kites or newSpecies. Add a count for the red kites, add them to the year list, and run the file.

03:18 The type arguments are erased with the other types, so Node creates an ordinary map and set. The output shows the map with a red kite count of 2, and the set with Red Kite.

03:30 A generic type names a kind of value, then says in angle brackets what it holds. Read it from the outside in. When a map or set starts empty, write its type arguments yourself.

03:44 Next, in Typing Async Functions, you’ll give the species lookups promise types like the one you read here.

Become a Member to join the conversation.