Parsing strings with template literal types
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Split a string type with infer and a template literal.
- Extract route parameters and event names.
- Recognize when parsing is too complex to be worth it.
Overview
#Matching a pattern
type Route<T> = T extends `/${infer Rest}` ? Rest : never;
type R = Route<"/users">; // "users"
The infer captures the part matched by the placeholder.
Splitting on a separator
type Split<S extends string> = S extends `${infer Head}/${infer Tail}`
? [Head, ...Split<Tail>]
: [S];
type Parts = Split<"a/b/c">; // ["a", "b", "c"]
Extracting parameters
type Param<T> = T extends `${string}:${infer Name}` ? Name : never;
type P = Param<"/users/:id">; // "id"
Combining with key remapping
Template parsing is how libraries type on plus a capitalized event name, and dotted object paths.
Limits
The checker matches template literals greedily and left to right, and deep recursion is expensive. For anything more than a few segments, prefer a runtime parser with a hand-written type.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Implement `Split<S extends string>` that splits a string type on `/` into a tuple.
starter.typescriptexport type Split<S extends string> = S extends `${infer Head}/${infer Tail}` ? [Head] : [S];Reveal the reference answer
reference.typescriptexport type Split<S extends string> = S extends `${infer Head}/${infer Tail}` ? [Head, ...Split<Tail>] : [S];Authored expected output: Compile-time: Split<"a/b/c"> is ["a", "b", "c"]. No runtime output.
infer splits off the first segment and recursion handles the rest.
Exercise 2Predict
What is `P`? ```ts type P = "/users/:id" extends `${string}:${infer Name}` ? Name : never; ```
Exercise 3Debug
This returns `never` for `/users`. Fix the pattern so it strips the leading slash. ```ts type Route<T> = T extends `/${infer Rest}/` ? Rest : never; ```
starter.typescripttype Route<T> = T extends `/${infer Rest}/` ? Rest : never;Reveal the reference answer
reference.typescripttype Route<T> = T extends `/${infer Rest}` ? Rest : never;Authored expected output: Compile-time: with the trailing slash removed, Route<"/users"> is "users".
The extra trailing slash made the pattern require a second slash that /users does not have.
Common misconceptions
#- Template literal types parse at runtime.
- infer matches the whole string.
- Any pattern is cheap to compute.
Follow-up practice
#- typescript-type-performance
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.