Essential Types And Notations 20 exercises

Type Checking Maps

Here we are creating a Map, which is essentially a dictionary.

In this case we want to pass in a number for the key, and an object for the value:

const userMap = new Map();
userMap.set(1, { name: "Max", age: 30 });
userMap.set(2, { name: "Manuel", age: 31 });
// @ts-expect-error

Loading exercise


00:00 Okay, a similar setup to last time. Now we have a map instead of a set and a map is slightly different because it's basically a dictionary. So it stores keys inside the map and then on those keys it adds a value. So here on the one key we're setting name Max age 30.

00:18 On the two key we're setting name Manuel age 31. Same with Anna down here except we're expecting an error because you can, in this particular map what we're using, you can only pass in number as the key. So your job is to, very similar to the set, is to try to

00:34 work out a way that we can type this map here. If we take a little look there you might see a couple of little type arguments there and you might have to do some guesswork to work out which is the key and which is the value. But I think I've given you all the information and maybe a bit too much for this exercise but I reckon you can do it. Good luck.