TypeScript · Senior · Question 9 of 75
What does Split<'a/b/c'> return?
type Split<S extends string> =
S extends `${infer Head}/${infer Rest}`
? [Head, ...Split<Rest>]
: [S];- ✓Correct answer:
['a', 'b', 'c'] - 2
string[](a general type) - 3
['a/b/c'](no parsing)
Explanation
infer inside a template literal parses the string: Head captures the part before the first /, Rest the remainder. Recursion and variadic tuples assemble the result. This is how route params like /users/:id are typed.