TypeScript Generics Workshop (6 exercises)

Add Type Parameters to a Function

In this exercise, we have a function called createSet which returns a Set which can only contain unique values:

We also have a stringSet, a numberSet, and an unknownSet that each pass in a type argument to createSet:

Loading exercise


[0:00] [0: 00] In this exercise, we have a function called createSet, and createSet just returns a Set. If you don't know what a Set is, it's just a part of JavaScript. It's like a unique set of values. We have a stringSet here, a numberSet and an unknownSet, and we're passing in a type argument here to createSet. OK, that's interesting.
[0:22] [0: 23] This stringSet we can see here it's supposed to be Set<string> but actually it's Set<unknown>. This one is supposed to be Set, but it's Set. This one, we're happy with it, being Set. Your job is to figure out what this syntax is all about and how we can change this function to make this syntax behave as expected.