This solution is generated by ChatGPT, correctness is not guaranteed. You might also be able to find a solution fromcommunity posts.
AI solution for TypeScript puzzle on BFE.dev
43. implement Trim<T>
type Trim<T extends string> = T extends `${' ' | '\t' | '\n'}${infer R}` | `${infer R}${' ' | '\t' | '\n'}` ? Trim<R> : T;
Explanation:
- We define a generic type
T
which extendsstring
as the input. - We use conditional types with
extends
to check if the string starts or ends with a space, tab, or newline character. If it does, we useinfer
to capture the rest of the string. - If the string starts or ends with one of those characters, we recursively call
Trim
using the captured rest of the string as the new input. - If the string doesn't start or end with one of those characters, we return the original input string.