0Pricing
TypeScript Academy · Lesson

String-pattern typing (route params, event names)

Constrain strings with template literal types: route parameter patterns, event name namespaces, and safe builders.

String-pattern typing (route params, event names) is a free TypeScript Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro

Goal: Model string formats in types. With template literal types you can enforce patterns like "/users/:id" or "user:created" while keeping great DX.

  • Constrain by prefix/suffix
  • Extract pieces with infer
  • Build safe helpers

Route params

A simple route template and infer the parameter name; never in cases that do not comply with the rule.

type Route = `/${string}`

type RouteWithParam = `/${string}/:${string}`

type ParamOf<R extends string> = R extends `${string}/:${infer P}` ? P : never

// examples
type A = ParamOf<"/users/:id">      // "id"
type B = ParamOf<"/posts/:slug">    // "slug"

Safe route helper

Safe builder: The helper function accepts templates that begin with "/"; others are rejected in compilation.

type Path<R extends string> = R extends `/${string}` ? R : never

function route<R extends Path<R>>(r: R) { return r }

const ok = route("/users/:id")    // ok
// const bad = route("users")      // error: must start with "/"

Event namespaces

Limit prefixes (e.g. user:, order:) and block bad field names with types.

type EventName = `user:${string}` | `order:${string}`

function on<E extends EventName>(name: E, cb: (data: unknown) => void) {
  // ... register listener
}

on("user:created", d => {})     // ok
// on("payment:done", d => {})  // error: not an EventName

Split event

Extract the two sections with infer and convert them to typed fields; invalid formats will never occur.

type SplitEvent<E extends string> = E extends `${infer Scope}:${infer Action}` ? { scope: Scope; action: Action } : never

type S = SplitEvent<"user:created">   // { scope: "user"; action: "created" }

type Bad = SplitEvent<"oops">          // never

Tips

Tips:

Keep constraints minimal; overly strict templates force refactoring.

Combine rules in one place with Builder functions.

Don't forget to map the underlying string to unknown data and validate it at runtime.

Namespaces check

Quick check: Which pattern constrains event names to two namespaces?

Recap

Recap: Restrict templated strings with template literal types; strip parts with infer; create safe builder functions.

Frequently asked questions

Is the “String-pattern typing (route params, event names)” lesson free?

Yes — the full text of “String-pattern typing (route params, event names)” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “String-pattern typing (route params, event names)”?

Constrain strings with template literal types: route parameter patterns, event name namespaces, and safe builders. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “String-pattern typing (route params, event names)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Mapped types with +/-readonly and +/-?
  2. Key remapping & template literal types
  3. String-pattern typing (route params, event names)
← Back to TypeScript Academy