Type Transformations Workshop (6 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:

In SplitPath, we are using S.Split, passing it the Path, and then setting the delimiter to be /.

From here, S.Split will split the Path string up on the forward slash.

Of course, we can set it to split on any character we want.

Doing this type of operation at the type level is pretty fascinating!

Keep Split in mind, especially if you're looking to do really clever stuff with dynamic path parameters (which we may explore later in the section).


[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.