Open utterances-bot opened 3 years ago
вот еще один вариант решения
type TrimLeft<S extends string> = S extends ` ${infer T}` | `${infer T} ` ? TrimLeft<T> : S;
type trimed = TrimLeft<' Hello World '> // expected to be 'Hello World '
type trimed2 = TrimLeft<' Hello World'> // expected to be 'Hello World '
type trimed3 = TrimLeft<'Hello World '> // expected to be 'Hello World '
@boggddan этот даже попроще будет, хороший трюк с объединением 👍🏻
Решение через union:
type TrimChars = ' ' | '\n' | '\t';
type Trim<S extends string> = S extends `${TrimChars}${infer R}` | `${infer R}${TrimChars}` ? Trim<R> : S;
Trim
This project is aimed at helping you better understand how the type system works, writing your own utilities, or just having fun with the challenges.
https://ghaiklor.github.io/type-challenges-solutions/ru/medium-trim.html