Beginner's TypeScript Tutorial (18 exercises)
Problem

Passing Type Arguments

For this exercise, we'll be working with the Set data structure from JavaScript.

Here we're creating a set of guitarists and adding Jimi Hendrix and Eric Clapton to it.

const guitarists = new Set();

guitarists.add("Jimi Hendrix");
guitarists.add("Eric Clapton");

We also have the // @ts-expect-error directive because we expect an error to be thrown we try to add a non-string to the set.

it("Should give a type error when you try to pass a non-string", () => {
  // @ts-expect-error
  guitarists.add(2);
});

However, our error isn't being thrown because the guitarists set is not strictly typed as a set of strings.

We see this same issue when hovering over the guitaristsAsArray variable inside of the array test:

const guitaristsAsArray = Array.from(guitarists);

Hovering shows us that guitaristsAsArray is an unknown array.

Challenge

Your challenge is to update guitarists to be typed a set of strings.

Transcript

Hello. I've changed my t-shirt. Now, we have a problem here which is to do with this set here. We are adding guitarists to this set. We're adding Jimi Hendrix to it and Eric Clapton. This set, by the way, if you've never seen it before, it's nothing to do with TypeScript. This is actually a JavaScript thing. We're expecting that guitarists should contain Jimi Hendrix and Eric Clapton, which they do.

We're also expecting an error to throw here when you add a non-string to that set. If I remove this, then this should be erroring here, but it's not. For some reason, our set isn't being strictly typed as a set of strings. We've got it here, too, "Should be typed as an array of strings."

When we get our guitaristsAsArray here, when we call Array.from guitarists, then it should be returning a type which is a string array, but it's not. It's saying it's unknown array. Your challenge is to figure out -- I'll give you a clue, it's on this line -- how to type this set as a set of strings and not just what it is now, which is a set unknown.