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
52. implement Split<S, D>
Solution:
type Split<S extends string, D extends string> = S extends `${infer L}${D}${infer R}` ? [L, ...Split<R, D>] : [S];
Explanation:
First, we define a generic type Split<S, D>
, where S
represents the input string, and D
represents the delimiter string. The goal is to split S
into an array of substrings, using D
as the delimiter.
We then use a conditional type to check if S
matches the pattern ${infer L}${D}${infer R}
. If it does, we extract the left and right substrings (using the infer
keyword) and recursively call Split
on the right substring R
.
We prepend the left substring L
to the resulting array, and return it as the output. If S
does not match the pattern, we simply return an array with S
as its only element (since there are no more delimiters to split on).
This implementation should correctly handle all the test cases provided.