Intro to Generics 9 exercises
Problem

Restricting Generic Argument Types

In this exercise, we start with the returnWhatIPassIn function from the previous exercise. It has the T and it's basically returning whatever we pass in:


const returnWhatIPassIn = <T>(t: T) => {
return t;
};

But now our test is saying it should only allow strings to be pass

Loading exercise

Transcript

0:00 In this exercise, we have our returnWhatIPassIn function from the previous exercise. We have our T, and it's basically returning what if we pass in, which is really nice.

0:09 Now our test is saying it should only allow strings to be passed in. This means that we want to actually constrain the type of T to make sure it's only a string.

0:19 We could of course do this, where we could say, "OK, you can pass in a string here," but actually, you end up with a being a string instead of the exact thing that you're passing in.

0:28 Your job is to find a way and find a method of doing this syntax so that the thing that we pass in must be a string but still gets inferred as its literal type.