← blog
TypeScriptNovember 27, 2026 · 6 min read

Template Literal Types Explained: Catching Path Typos at Compile Time

A typo'd dot-path like "theme.colros.primary" compiles clean and fails silently at runtime — template literal types make TypeScript catch it before you ship.

Parsa Jiravand · Frontend engineer · building bestpractic
Template Literal Types Explained: Catching Path Typos at Compile Time

Somewhere in your codebase there's a function that looks like this:

TypeScript
1
2
3
4
5
function get(obj: any, path: string) { return path.split(".").reduce((acc, key) => acc?.[key], obj); } const primaryColor = get(config, "theme.colros.primary");

Read that path again. colros, not colors. TypeScript compiles it without a single complaint. The app ships. primaryColor is undefined, some fallback kicks in, and the button is the wrong shade of blue for three weeks before a designer notices and files a ticket nobody can reproduce on the first try.

The type system had everything it needed to catch this. config has a real, known shape. The bug is a four-character typo in a string literal sitting right there in the call site. And TypeScript let it through anyway — because as far as the compiler is concerned, path: string accepts literally any string.

Here's the part that's easy to miss: fixing this isn't a runtime check. It's a type you can write yourself, today, in about fifteen lines, using a feature TypeScript has shipped since 4.1.

The naive get function above type-checks because string is the type. Every string is a string. TypeScript has no way to know that "theme.colros.primary" isn't a real path on config unless something narrower than string is doing the asking.

You could reach for keyof to type a single level:

TypeScript
1
2
3
function getTop<T, K extends keyof T>(obj: T, key: K): T[K] { return obj[key]; }

That's genuinely safe — getTop(config, "theem") is a compile error. But it only goes one property deep. The moment you want "theme.colors.primary", you're back to a plain string, because keyof T gives you "theme", not "theme.colors.primary".

What you actually want is a type that looks at T and produces the union of every valid dot-path through it — and that's exactly what template literal types, combined with recursive conditional types, build.

Template literal types let you glue string literal types together the way you'd glue strings with ${} in a template string, except it happens at the type level:

TypeScript
1
2
type Greeting = `Hello, ${string}`; // matches "Hello, world", "Hello, ", but not "Hi there"

That's the primitive. The trick is combining it with a mapped type over keyof T that recurses into each property:

TypeScript
1
2
3
4
5
6
7
8
9
10
11
type Primitive = string | number | boolean | bigint | symbol | undefined | null | Date; type Paths<T> = T extends Primitive ? never : T extends readonly unknown[] ? never : { [K in keyof T & string]: T[K] extends Primitive ? K : K | `${K}.${Paths<T[K]>}`; }[keyof T & string];

Read it as: for every key K on T, if the value at that key is a primitive, the path stops there — just K. If it's an object, the path is either K itself or K followed by a dot and a valid path through that nested object. The [keyof T & string] at the end collapses the mapped type into a plain union.

Give it this Config:

TypeScript
1
2
3
4
5
6
7
interface Config { theme: { colors: { primary: string; secondary: string }; darkMode: boolean; }; name: string; }

Paths<Config> resolves to:

TypeScript
"name" | "theme" | "theme.colors" | "theme.darkMode" | "theme.colors.primary" | "theme.colors.secondary"

Every single one of those strings is a real path on a real Config. There's no way to spell a typo and have it type-check, because the union was generated from the object's own declared shape, not typed by hand.

The path type alone doesn't fix get — you still need the return type to follow the path, or every call collapses to unknown. That's a second recursive conditional type, walking the string apart instead of the object:

TypeScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
type PathValue<T, P extends string> = P extends `${infer Head}.${infer Rest}` ? Head extends keyof T ? PathValue<T[Head], Rest> : never : P extends keyof T ? T[P] : never; function get<T, P extends Paths<T>>(obj: T, path: P): PathValue<T, P> { const parts = (path as string).split("."); let current: unknown = obj; for (const part of parts) current = (current as Record<string, unknown>)?.[part]; return current as PathValue<T, P>; }

PathValue pattern-matches the string literal P against `${infer Head}.${infer Rest}`. If there's a dot, it peels off the first segment and recurses on the rest; once there's no dot left, it looks the final segment up with T[P] directly.

Now run the typo through it:

TypeScript
1
2
get(config, "theme.colors.primary"); // string — correct, and TypeScript knows it get(config, "theme.colros.primary"); // compile error, not a runtime surprise

That second line doesn't run. It doesn't ship. tsc stops it with:

Text
1
2
Argument of type '"theme.colros.primary"' is not assignable to parameter of type '"name" | "theme.colors.primary" | "theme" | "theme.colors" | "theme.darkMode" | "theme.colors.secondary"'.

I ran both versions through tsc --strict before writing this — the naive one takes the typo silently, the typed one refuses to compile it. That's the entire payoff: the same information that used to surface as a confused designer three weeks later now surfaces as a red squiggle while you're still typing the call.

If you've ever typed a field name into react-hook-form's register("user.address.city") and had autocomplete walk you through it, you've already used a type built this same way — their Path<T> helper exists to solve exactly this problem for form fields.

Two honest limits, because a type that silently falls back to any on hard cases is worse than no type at all.

Depth. TypeScript caps how deeply it will recurse into a conditional type. For most app config, form, or API-response shapes — three or four levels of nesting — Paths<T> resolves fast and correctly. Push it at a genuinely deep, non-recursive chain (I tested around sixty levels) and you'll hit Type instantiation is excessively deep and possibly infinite. At that point the fix is a depth-limited variant (cap the recursion with a counter type), not pushing harder on the same approach.

A type that references itself behaves differently, and worse. Give Paths an interface TreeNode { value: string; children: TreeNode } and TypeScript refuses outright with a different error — Type of property 'children' circularly references itself in mapped type — because the mapped type can't even start resolving a property whose type is itself. But the shape real trees actually use, children: TreeNode[], doesn't error at all: Paths<T> already bails out on arrays, so Paths<TreeNode> quietly resolves to just "value" | "children" — technically correct, but silently blind to everything nested under children. That's the failure mode worth losing sleep over: not a loud compiler error, but a type that compiles clean and just stops short of where you assumed it reached.

Arrays. The Paths<T> above explicitly bails on arrays (T extends readonly unknown[] ? never) rather than pretending to support them. Indexing into arrays by position (items.0.name) is a real, solvable extension — using `${number}` as a key — but it's a deliberately separate problem from object paths, and bolting it on half-finished is how you end up with a type that compiles but lies.

Neither of these is a reason to skip the type. They're the reason to know, before you reach for it on a 12-level GraphQL response, that you're past where it comfortably lives.

Runs right in your browser — poke at it and watch the concept react live.

The next time you write obj[a][b][c] with a string you typed from memory, ask yourself whether TypeScript actually checked it — or just trusted you. What's the last path typo that made it past your compiler and into production? I'd genuinely like to know I'm not the only one.

Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.


🚀 Want more like this? Every guide, playground, and quiz lives on bestpractic.org — open it and sign up free so the next one finds you.

Thanks for reading! Let's stay connected:

Keep reading

One post a day, in your inbox

Each one with a runnable playground and a quiz. No pitch, no digest, unsubscribe in one click.

0 comments

Sign in to join the discussion, like comments, and save articles for later.