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];

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.

Further reading

Problem with a question

TypeScript · Senior · question 9

Test yourself

Take the TypeScript · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz