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
54. Implement CamelCase<S>
type CamelCase<S> = S extends `${infer T}_${infer U}` ? `${Capitalize<T>}${CamelCase<U>}` : Capitalize<S>;
Explanation:
- We start by checking if the input string
S
contains an underscore_
. We do this using a conditional type and the syntax${infer T}_${infer U}
which splits the string into two partsT
andU
separated by an underscore. - If the input string contains an underscore, we capitalize the first part
T
usingCapitalize<T>
, and then recursively callCamelCase
on the second partU
. - Otherwise, if the input string does not contain an underscore, we simply capitalize the entire string using
Capitalize<S>
.