Trim Left

Example of TrimLeft use
Example of TrimLeft use

Today we discuss Trim Left

String.prototype.trimStart does the same in JavaScript what we are about to do in TypeScript.

Let's try 👩‍💻

Iteration over a string

We already know how to iterate over:

For strings we need several concepts:

  1. Type inference in conditional types to infer substrings without whitespaces.
  2. Template Literal types to match substrings in a string.

First, let's declare Whitespace which will be removed from the beginning:

Whitespace example
Whitespace example

Second, let's try to remove the first whitespace if it is a part of a string we pass. We will get something like this:

Example with removing one whitespace
Example with removing one whitespace

Not all test cases are completed because we don't remove whitespaces after the first one. Let's apply the type recursively:


That's it 💪

The solution is available in Playground

Have a good day and week 🌤

Alexey Berezin profile image

Written by Alexey Berezin who loves London 🏴󠁧󠁢󠁥󠁮󠁧󠁿, players ⏯ and TypeScript 🦺 Follow me on Twitter