Go Back

Refer: Medium - Trim Left

Describe

Implement TrimLeft<T> which takes an exact string type and returns a new string with the whitespace beginning removed.

For example

code snippetCopytypescript
type trimed = TrimLeft<' Hello World '> // expected to be 'Hello World '

Test Cases

code snippetCopytypescript
import { Equal, Expect } from '@type-challenges/utils' type cases = [ Expect<Equal<TrimLeft<'str'>, 'str'>>, Expect<Equal<TrimLeft<' str'>, 'str'>>, Expect<Equal<TrimLeft<' str'>, 'str'>>, Expect<Equal<TrimLeft<' str '>, 'str '>>, Expect<Equal<TrimLeft<' \n\t foo bar '>, 'foo bar '>>, ]

Solution

code snippetCopytypescript
type TrimLeft<S extends string> = S extends `${' ' | '\n' | '\t'}${infer P}` ? TrimLeft<P> : S