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
Twhich extendsstringas the input. - We use conditional types with
extendsto check if the string starts or ends with a space, tab, or newline character. If it does, we useinferto capture the rest of the string. - If the string starts or ends with one of those characters, we recursively call
Trimusing 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.