Template Literals 7 exercises

Extract Union Strings Matching a Pattern

In this exercise we have a union of Routes where some of the strings have a colon (:):

type Routes = "/users" | "/users/:id" | "/posts" | "/posts/:id";

The strings with a : allow for dynamic path parameters.


Your challenge is to update DynamicRoutes to extract me

Loading exercise


0:00 Here, we need to use template literals combined with another piece of syntax that we've seen before. The name of this exercise might give you a clue. What we're trying to do is we've got these routes here. Some of the members of this union have a colon in them. If they have a colon in them, that indicates that it's a dynamic route.

0:21 In other words, you can pass like users one, users two. These are dynamic path parameters. What we want to do is end up with a situation where DynamicRoutes is equal to the members of Routes that have a colon in them. That will mean that we can add members to this, and DynamicRoutes will update.

0:40 Because if I just do users id here or posts id, then it's going to pass, but if I add a new one to this, then it's not going to pass. That's your goal, is to make DynamicRoutes...We want to just extract out of Routes the members that conform to this sort of string.