Type Transformations Workshop (6 exercises)

Splitting A String into a Tuple

Let's dive deeper into template literals by looking at splitting a string at the type level.

We start with a type named Path that just is expressed as a string literal:


Your challenge is to update SplitPath to be a tuple of the parts of Path between the slashes. A tuple is similar to an array, but will have specific elements as seen in the test:

Inside of the ts-toolbelt library is a tool called S that can help you with this.

Check out the docs for S to learn how to get started, and we'll examine further in the solution.


[0:00] Let's dive deeper into template literals. We're now going to try and split a string at the type level.
[0:07] We have a Path here inside a type, which is just being expressed as a string literal. We want SplitPath to equal that path but split into all of its constituent bits inside a tuple.

[0:21] You see that this is basically an array here, except it's an array of certain named elements, and that's what we want it to look like.

[0:29] There is a tool that can help you with this, which is S ts-toolbelt. There's a link to some documentation here, which we'll explore in the solution, to show you a little really useful tool to help you get this done.