Generic Functions Without Inference
Here we have a function createStringMap
. The purpose of this function is to generate a Map
with keys as strings and values of the type passed in as arguments:
const createStringMap = () => { return new Map();};
As it currently is, keys and values can be of any type.
However, th
Transcript
00:00 In this exercise, we're going to look at how you can pass type arguments to functions to tell the function what it's supposed to be doing. We have here a function called createStringMap. What createStringMap is supposed to be doing is that we can basically say, okay, we create a map from the end of here and the map is supposed to be a map where the strings are the keys
00:18 and the type that you pass in is basically the type of the values here. Except what we're getting back here is a map containing any and any here. So it seems like the new map that we're creating here just doesn't seem to be doing what we want it to do. Your job is to work out a way where we can add in a type parameter to createStringMap,
00:38 basically making it a generic function. When we make it a generic function, we'll be able to use that type inside the function's scope and essentially use it to type our map for us. That's fully it. We need to make sure it works for these two cases where we have passing in the number and the number should be the value,
00:56 or we pass in any object, or rather this object with a as a number, and then that should be the value there too. I think you have all the information. I'll provide some links below. Good luck.