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 snippetCopytypescripttype trimed = TrimLeft<' Hello World '> // expected to be 'Hello World '
Test Cases
code snippetCopytypescriptimport { 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 snippetCopytypescripttype TrimLeft<S extends string> = S extends `${' ' | '\n' | '\t'}${infer P}` ? TrimLeft<P> : S