Template Literals 7 exercises

Using S From ts-toolbelt to Split a String Into a Tuple

The documentation for ,S.Split,. is fairly limited, so we'll read it through our code.

Here's the solution:

type SplitPath = S.Split<Path, "/">

In SplitPath, we are using `S.S

Loading solution


0:00 The page of documentation that this takes you to is S.Split here. This is pretty limited in terms of docs, so it's better to actually read what it does here.

0:10 What we're doing is we have a S.Split that we're importing from ts-toolbelt, and we're grabbing the Path here and passing in a /. That forward slash then splits that path by that delimiter.

0:26 We can choose a different delimiter if we want to by having this "s" here and "U", "er", "/John/Document", "/note", ".txt," and so anything works here. It's fascinating that you can do this at the type level.

0:39 If we look into Split itself, it's a pretty scary type declaration, and this is beyond the scope of the course, really, to examine most of this stuff, especially beyond the scope of this module.

0:52 You should know that this is available to you, especially if you're looking to do really clever stuff with dynamic path parameters, which we may explore later in the section.