[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"verticals":3,"quiz-typescript-template-literal-types":44,"search-suggestions":60,"quiz-article-typescript-template-literal-types":109,"path-membership-typescript-template-literal-types":328},[4,20,32],{"id":5,"slug":6,"name":7,"tagline":8,"description":9,"accentFrom":10,"accentTo":11,"icon":12,"defaultLocale":13,"locales":14,"features":16,"position":19},"019fe637-3d33-714b-b57f-23e163ffca0c","dev","Web Development","Read it. Run it. Prove it.","A post a day on modern web development — most with an editable playground and a quiz that explains every answer. Free, no account needed.","violet-500","cyan-400","◇","en",[13,15],"fa",{"courses":17,"paths":17,"articles":17,"exams":18,"flashcards":18,"packages":17,"community":17,"certificates":17,"teams":17,"commerce":17},true,false,0,{"id":21,"slug":22,"name":23,"tagline":24,"description":25,"accentFrom":26,"accentTo":10,"icon":27,"defaultLocale":13,"locales":28,"features":30,"position":31},"019fe637-3dc2-754c-8657-0f175bfee7c6","lang","Languages","Learn a language the way you learn a codebase.","Grammar explained the way good documentation explains an API — one idea at a time, each with a quiz.","amber-400","⌘",[13,15,29],"es",{"courses":18,"paths":18,"articles":17,"exams":18,"flashcards":17,"packages":18,"community":17,"certificates":17,"teams":18,"commerce":18},2,{"id":33,"slug":34,"name":35,"tagline":36,"description":37,"accentFrom":38,"accentTo":39,"icon":40,"defaultLocale":13,"locales":41,"features":42,"position":43},"7b3c16f2-931d-410e-802e-e1fa4edab7de","soft","Soft Skills","The half of the job nobody wrote documentation for.","Weekly, on the parts of working life that decide more than your code does — first weeks, meetings, interviews, promotions, and the people around you. Written from what actually happens, and recorded as a podcast you can listen to on the walk.","emerald-400","teal-300","◉",[13],{"courses":18,"paths":18,"articles":17,"exams":18,"flashcards":18,"packages":18,"community":17,"certificates":18,"teams":18,"commerce":18},3,{"id":45,"slug":46,"kind":47,"title":48,"description":49,"config":50,"verticalId":5,"vertical":55,"course":52,"_count":56,"access":57,"attempts":59,"questionCount":51},"01a1fce0-db1d-7319-b048-2c28003dafe4","typescript-template-literal-types","PRACTICE_QUIZ","Template literal types — type-safe object paths","Checks whether you can explain why a plain `string` path typo slips through, and how `Paths\u003CT>` and `PathValue\u003CT, P>` close that gap — plus where the approach hits real limits.",{"questionCount":51,"timeLimitSec":52,"shuffleQuestions":18,"shuffleOptions":17,"negativeMarking":19,"passScorePct":53,"maxAttempts":52,"revealAnswers":54,"allowFlagging":18,"allowBacktracking":17},8,null,70,"AFTER_SUBMIT",{"slug":6,"name":7},{"questions":51},{"allowed":17,"reason":58},"FREE",[],[61,65,69,73,77,81,85,89,93,97,101,105],{"slug":62,"name":63,"articles":64},"webdev","Webdev",195,{"slug":66,"name":67,"articles":68},"javascript","Javascript",156,{"slug":70,"name":71,"articles":72},"frontend","Frontend",103,{"slug":74,"name":75,"articles":76},"tutorial","Tutorial",85,{"slug":78,"name":79,"articles":80},"css","Css",56,{"slug":82,"name":83,"articles":84},"typescript","Typescript",36,{"slug":86,"name":87,"articles":88},"performance","Performance",33,{"slug":90,"name":91,"articles":92},"programming","Programming",26,{"slug":94,"name":95,"articles":96},"react","React",23,{"slug":98,"name":99,"articles":100},"node","Node",19,{"slug":102,"name":103,"articles":104},"browser","Browser",18,{"slug":106,"name":107,"articles":108},"nextjs","Nextjs",13,{"id":110,"slug":46,"title":111,"subtitle":52,"excerpt":112,"coverUrl":113,"locale":13,"readingMinutes":114,"publishedAt":115,"viewCount":116,"likeCount":19,"commentCount":19,"author":117,"vertical":122,"topic":123,"tags":125,"_count":130,"playground":132,"body":134,"bodyMd":315,"seo":316,"translationGroupId":318,"series":52,"podcastUrl":52,"verticalId":5,"thread":319,"assessments":321,"translations":324,"quiz":326},"01a1fce0-dac8-71e2-bd25-94b1cc5aa337","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.","\u002Fmedia\u002Fcovers\u002Ftypescript-template-literal-types.png",6,"2026-11-27T11:49:01.332Z",27,{"id":118,"name":119,"username":120,"avatarUrl":52,"headline":121},"019fe637-3c25-7088-9034-39c9f15dc3c8","Parsa Jiravand","parsa","Frontend engineer · building bestpractic",{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":82,"name":124},"TypeScript",[126,127,128,129],{"slug":82,"name":83,"color":52},{"slug":66,"name":67,"color":52},{"slug":62,"name":63,"color":52},{"slug":74,"name":75,"color":52},{"assessments":131},1,{"slug":46,"title":133},"Template literal types — type-safe path checker",{"blocks":135,"version":131},[136,140,146,149,152,155,160,163,166,170,173,176,179,182,186,189,193,196,199,203,206,210,213,217,220,224,227,230,234,237,242,245,248,251,254,257,260,263,266,269,272,275,278,281,284,287,290,293,296,299,302,305,308],{"id":137,"html":138,"type":139},"b1","\u003Cp>Somewhere in your codebase there&#39;s a function that looks like this:\u003C\u002Fp>","paragraph",{"id":141,"code":142,"type":143,"language":144,"highlight":145},"b2","function get(obj: any, path: string) {\n  return path.split(\".\").reduce((acc, key) => acc?.[key], obj);\n}\n\nconst primaryColor = get(config, \"theme.colros.primary\");","code","ts",[],{"id":147,"html":148,"type":139},"b3","\u003Cp>Read that path again. \u003Ccode>colros\u003C\u002Fcode>, not \u003Ccode>colors\u003C\u002Fcode>. TypeScript compiles it without a single complaint. The app ships. \u003Ccode>primaryColor\u003C\u002Fcode> is \u003Ccode>undefined\u003C\u002Fcode>, 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.\u003C\u002Fp>",{"id":150,"html":151,"type":139},"b4","\u003Cp>The type system had everything it needed to catch this. \u003Ccode>config\u003C\u002Fcode> 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, \u003Ccode>path: string\u003C\u002Fcode> accepts literally any string.\u003C\u002Fp>",{"id":153,"html":154,"type":139},"b5","\u003Cp>Here&#39;s the part that&#39;s easy to miss: fixing this isn&#39;t a runtime check. It&#39;s a type you can write yourself, today, in about fifteen lines, using a feature TypeScript has shipped since 4.1.\u003C\u002Fp>",{"id":156,"html":157,"text":158,"type":159,"level":31},"b6","Why \u003Ccode>path: string\u003C\u002Fcode> can&#39;t save you","Why path: string can't save you","heading",{"id":161,"html":162,"type":139},"b7","\u003Cp>The naive \u003Ccode>get\u003C\u002Fcode> function above type-checks because \u003Ccode>string\u003C\u002Fcode> is the type. Every string is a \u003Ccode>string\u003C\u002Fcode>. TypeScript has no way to know that \u003Ccode>&quot;theme.colros.primary&quot;\u003C\u002Fcode> isn&#39;t a real path on \u003Ccode>config\u003C\u002Fcode> unless something narrower than \u003Ccode>string\u003C\u002Fcode> is doing the asking.\u003C\u002Fp>",{"id":164,"html":165,"type":139},"b8","\u003Cp>You could reach for \u003Ccode>keyof\u003C\u002Fcode> to type a single level:\u003C\u002Fp>",{"id":167,"code":168,"type":143,"language":144,"highlight":169},"b9","function getTop\u003CT, K extends keyof T>(obj: T, key: K): T[K] {\n  return obj[key];\n}",[],{"id":171,"html":172,"type":139},"b10","\u003Cp>That&#39;s genuinely safe — \u003Ccode>getTop(config, &quot;theem&quot;)\u003C\u002Fcode> is a compile error. But it only goes one property deep. The moment you want \u003Ccode>&quot;theme.colors.primary&quot;\u003C\u002Fcode>, you&#39;re back to a plain \u003Ccode>string\u003C\u002Fcode>, because \u003Ccode>keyof T\u003C\u002Fcode> gives you \u003Ccode>&quot;theme&quot;\u003C\u002Fcode>, not \u003Ccode>&quot;theme.colors.primary&quot;\u003C\u002Fcode>.\u003C\u002Fp>",{"id":174,"html":175,"type":139},"b11","\u003Cp>What you actually want is a type that looks at \u003Ccode>T\u003C\u002Fcode> and produces the union of \u003Cem>every valid dot-path through it\u003C\u002Fem> — and that&#39;s exactly what template literal types, combined with recursive conditional types, build.\u003C\u002Fp>",{"id":177,"html":178,"text":178,"type":159,"level":31},"b12","Building the path type",{"id":180,"html":181,"type":139},"b13","\u003Cp>Template literal types let you glue string literal types together the way you&#39;d glue strings with \u003Ccode>${}\u003C\u002Fcode> in a template string, except it happens at the type level:\u003C\u002Fp>",{"id":183,"code":184,"type":143,"language":144,"highlight":185},"b14","type Greeting = `Hello, ${string}`;\n\u002F\u002F matches \"Hello, world\", \"Hello, \", but not \"Hi there\"",[],{"id":187,"html":188,"type":139},"b15","\u003Cp>That&#39;s the primitive. The trick is combining it with a \u003Cstrong>mapped type over \u003Ccode>keyof T\u003C\u002Fcode>\u003C\u002Fstrong> that recurses into each property:\u003C\u002Fp>",{"id":190,"code":191,"type":143,"language":144,"highlight":192},"b16","type Primitive = string | number | boolean | bigint | symbol | undefined | null | Date;\n\ntype Paths\u003CT> = T extends Primitive\n  ? never\n  : T extends readonly unknown[]\n    ? never\n    : {\n        [K in keyof T & string]: T[K] extends Primitive\n          ? K\n          : K | `${K}.${Paths\u003CT[K]>}`;\n      }[keyof T & string];",[],{"id":194,"html":195,"type":139},"b17","\u003Cp>Read it as: for every key \u003Ccode>K\u003C\u002Fcode> on \u003Ccode>T\u003C\u002Fcode>, if the value at that key is a primitive, the path stops there — just \u003Ccode>K\u003C\u002Fcode>. If it&#39;s an object, the path is either \u003Ccode>K\u003C\u002Fcode> itself or \u003Ccode>K\u003C\u002Fcode> followed by a dot and a valid path \u003Cem>through that nested object\u003C\u002Fem>. The \u003Ccode>[keyof T &amp; string]\u003C\u002Fcode> at the end collapses the mapped type into a plain union.\u003C\u002Fp>",{"id":197,"html":198,"type":139},"b18","\u003Cp>Give it this \u003Ccode>Config\u003C\u002Fcode>:\u003C\u002Fp>",{"id":200,"code":201,"type":143,"language":144,"highlight":202},"b19","interface Config {\n  theme: {\n    colors: { primary: string; secondary: string };\n    darkMode: boolean;\n  };\n  name: string;\n}",[],{"id":204,"html":205,"type":139},"b20","\u003Cp>\u003Ccode>Paths&lt;Config&gt;\u003C\u002Fcode> resolves to:\u003C\u002Fp>",{"id":207,"code":208,"type":143,"language":144,"highlight":209},"b21","\"name\" | \"theme\" | \"theme.colors\" | \"theme.darkMode\" | \"theme.colors.primary\" | \"theme.colors.secondary\"",[],{"id":211,"html":212,"type":139},"b22","\u003Cp>Every single one of those strings is a real path on a real \u003Ccode>Config\u003C\u002Fcode>. There&#39;s no way to spell a typo and have it type-check, because the union was generated \u003Cem>from the object&#39;s own declared shape\u003C\u002Fem>, not typed by hand.\u003C\u002Fp>",{"id":214,"html":215,"text":216,"type":159,"level":31},"b23","Making \u003Ccode>get\u003C\u002Fcode> use it","Making get use it",{"id":218,"html":219,"type":139},"b24","\u003Cp>The path type alone doesn&#39;t fix \u003Ccode>get\u003C\u002Fcode> — you still need the return type to follow the path, or every call collapses to \u003Ccode>unknown\u003C\u002Fcode>. That&#39;s a second recursive conditional type, walking the string apart instead of the object:\u003C\u002Fp>",{"id":221,"code":222,"type":143,"language":144,"highlight":223},"b25","type PathValue\u003CT, P extends string> = P extends `${infer Head}.${infer Rest}`\n  ? Head extends keyof T\n    ? PathValue\u003CT[Head], Rest>\n    : never\n  : P extends keyof T\n    ? T[P]\n    : never;\n\nfunction get\u003CT, P extends Paths\u003CT>>(obj: T, path: P): PathValue\u003CT, P> {\n  const parts = (path as string).split(\".\");\n  let current: unknown = obj;\n  for (const part of parts) current = (current as Record\u003Cstring, unknown>)?.[part];\n  return current as PathValue\u003CT, P>;\n}",[],{"id":225,"html":226,"type":139},"b26","\u003Cp>\u003Ccode>PathValue\u003C\u002Fcode> pattern-matches the string literal \u003Ccode>P\u003C\u002Fcode> against \u003Ccode>`${infer Head}.${infer Rest}`\u003C\u002Fcode>. If there&#39;s a dot, it peels off the first segment and recurses on the rest; once there&#39;s no dot left, it looks the final segment up with \u003Ccode>T[P]\u003C\u002Fcode> directly.\u003C\u002Fp>",{"id":228,"html":229,"type":139},"b27","\u003Cp>Now run the typo through it:\u003C\u002Fp>",{"id":231,"code":232,"type":143,"language":144,"highlight":233},"b28","get(config, \"theme.colors.primary\"); \u002F\u002F string — correct, and TypeScript knows it\nget(config, \"theme.colros.primary\"); \u002F\u002F compile error, not a runtime surprise",[],{"id":235,"html":236,"type":139},"b29","\u003Cp>That second line doesn&#39;t run. It doesn&#39;t ship. \u003Ccode>tsc\u003C\u002Fcode> stops it with:\u003C\u002Fp>",{"id":238,"code":239,"type":143,"language":240,"highlight":241},"b30","Argument of type '\"theme.colros.primary\"' is not assignable to parameter of type\n'\"name\" | \"theme.colors.primary\" | \"theme\" | \"theme.colors\" | \"theme.darkMode\" | \"theme.colors.secondary\"'.","plain",[],{"id":243,"html":244,"type":139},"b31","\u003Cp>I ran both versions through \u003Ccode>tsc --strict\u003C\u002Fcode> before writing this — the naive one takes the typo silently, the typed one refuses to compile it. That&#39;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&#39;re still typing the call.\u003C\u002Fp>",{"id":246,"html":247,"type":139},"b32","\u003Cp>If you&#39;ve ever typed a field name into react-hook-form&#39;s \u003Ccode>register(&quot;user.address.city&quot;)\u003C\u002Fcode> and had autocomplete walk you through it, you&#39;ve already used a type built this same way — their \u003Ccode>Path&lt;T&gt;\u003C\u002Fcode> helper exists to solve exactly this problem for form fields.\u003C\u002Fp>",{"id":249,"html":250,"text":250,"type":159,"level":31},"b33","Where this gets expensive",{"id":252,"html":253,"type":139},"b34","\u003Cp>Two honest limits, because a type that silently falls back to \u003Ccode>any\u003C\u002Fcode> on hard cases is worse than no type at all.\u003C\u002Fp>",{"id":255,"html":256,"type":139},"b35","\u003Cp>\u003Cstrong>Depth.\u003C\u002Fstrong> 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 — \u003Ccode>Paths&lt;T&gt;\u003C\u002Fcode> resolves fast and correctly. Push it at a genuinely deep, non-recursive chain (I tested around sixty levels) and you&#39;ll hit \u003Ccode>Type instantiation is excessively deep and possibly infinite\u003C\u002Fcode>. At that point the fix is a depth-limited variant (cap the recursion with a counter type), not pushing harder on the same approach.\u003C\u002Fp>",{"id":258,"html":259,"type":139},"b36","\u003Cp>A type that references \u003Cem>itself\u003C\u002Fem> behaves differently, and worse. Give \u003Ccode>Paths\u003C\u002Fcode> an \u003Ccode>interface TreeNode { value: string; children: TreeNode }\u003C\u002Fcode> and TypeScript refuses outright with a different error — \u003Ccode>Type of property &#39;children&#39; circularly references itself in mapped type\u003C\u002Fcode> — because the mapped type can&#39;t even start resolving a property whose type is itself. But the shape real trees actually use, \u003Ccode>children: TreeNode[]\u003C\u002Fcode>, doesn&#39;t error at all: \u003Ccode>Paths&lt;T&gt;\u003C\u002Fcode> already bails out on arrays, so \u003Ccode>Paths&lt;TreeNode&gt;\u003C\u002Fcode> quietly resolves to just \u003Ccode>&quot;value&quot; | &quot;children&quot;\u003C\u002Fcode> — technically correct, but silently blind to everything nested under \u003Ccode>children\u003C\u002Fcode>. That&#39;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.\u003C\u002Fp>",{"id":261,"html":262,"type":139},"b37","\u003Cp>\u003Cstrong>Arrays.\u003C\u002Fstrong> The \u003Ccode>Paths&lt;T&gt;\u003C\u002Fcode> above explicitly bails on arrays (\u003Ccode>T extends readonly unknown[] ? never\u003C\u002Fcode>) rather than pretending to support them. Indexing into arrays by position (\u003Ccode>items.0.name\u003C\u002Fcode>) is a real, solvable extension — using \u003Ccode>`${number}`\u003C\u002Fcode> as a key — but it&#39;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.\u003C\u002Fp>",{"id":264,"html":265,"type":139},"b38","\u003Cp>Neither of these is a reason to skip the type. They&#39;re the reason to know, before you reach for it on a 12-level GraphQL response, that you&#39;re past where it comfortably lives.\u003C\u002Fp>",{"id":267,"html":268,"type":139},"b39","\u003C!-- playground:start -->",{"id":270,"html":271,"text":271,"type":159,"level":31},"b40","🎮 Try it yourself",{"id":273,"html":274,"type":139},"b41","\u003Cp>\u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Ftypescript-template-literal-types\u002Fplayground\">▶️ Open the interactive playground →\u003C\u002Fa>\u003C\u002Fstrong>\u003C\u002Fp>",{"id":276,"html":277,"type":139},"b42","\u003Cp>\u003Cem>Runs right in your browser — poke at it and watch the concept react live.\u003C\u002Fem>\u003C\u002Fp>",{"id":279,"html":280,"type":139},"b43","\u003C!-- playground:end -->",{"id":282,"html":283,"type":139},"b44","\u003Cp>The next time you write \u003Ccode>obj[a][b][c]\u003C\u002Fcode> with a string you typed from memory, ask yourself whether TypeScript actually checked it — or just trusted you. What&#39;s the last path typo that made it past your compiler and into production? I&#39;d genuinely like to know I&#39;m not the only one.\u003C\u002Fp>",{"id":285,"html":286,"type":139},"b45","\u003C!-- quiz:start -->",{"id":288,"html":289,"text":289,"type":159,"level":31},"b46","🧠 Test yourself",{"id":291,"html":292,"type":139},"b47","\u003Cp>Think it clicked? \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Ftypescript-template-literal-types\u002Fquiz\">Take the 8-question quiz →\u003C\u002Fa>\u003C\u002Fstrong>\u003C\u002Fp>",{"id":294,"html":295,"type":139},"b48","\u003Cp>\u003Cem>Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.\u003C\u002Fem>\u003C\u002Fp>",{"id":297,"html":298,"type":139},"b49","\u003C!-- quiz:end -->",{"id":300,"type":301},"b50","divider",{"id":303,"html":304,"type":139},"b51","\u003Cp>🚀 \u003Cstrong>Want more like this?\u003C\u002Fstrong> Every guide, playground, and quiz lives on \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002F\">bestpractic.org\u003C\u002Fa>\u003C\u002Fstrong> — open it and \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002F\">sign up free\u003C\u002Fa>\u003C\u002Fstrong> so the next one finds you.\u003C\u002Fp>",{"id":306,"html":307,"type":139},"b52","\u003Cp>\u003Cem>Thanks for reading! Let&#39;s stay connected:\u003C\u002Fem>\u003C\u002Fp>",{"id":309,"type":310,"items":311,"ordered":18},"b53","list",[312,313,314],"⭐ \u003Cstrong>GitHub\u003C\u002Fstrong> — follow me and star the projects: \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fparsajiravand\">github.com\u002Fparsajiravand\u003C\u002Fa>","💬 \u003Cstrong>Discord\u003C\u002Fstrong> — join the frontend best-practices community: \u003Ca href=\"https:\u002F\u002Fdiscord.gg\u002Fd9KRhuAwQ\">discord.gg\u002Fd9KRhuAwQ\u003C\u002Fa>","📸 \u003Cstrong>Instagram\u003C\u002Fstrong> — frontend best practices, daily: \u003Ca href=\"https:\u002F\u002Fwww.instagram.com\u002Fbestpractice___\u002F\">@bestpractice___\u003C\u002Fa>","Somewhere in your codebase there's a function that looks like this:\n\n```ts\nfunction get(obj: any, path: string) {\n  return path.split(\".\").reduce((acc, key) => acc?.[key], obj);\n}\n\nconst primaryColor = get(config, \"theme.colros.primary\");\n```\n\nRead 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.\n\nThe 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.\n\nHere'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.\n\n## Why `path: string` can't save you\n\nThe 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.\n\nYou could reach for `keyof` to type a single level:\n\n```ts\nfunction getTop\u003CT, K extends keyof T>(obj: T, key: K): T[K] {\n  return obj[key];\n}\n```\n\nThat'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\"`.\n\nWhat 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.\n\n## Building the path type\n\nTemplate 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:\n\n```ts\ntype Greeting = `Hello, ${string}`;\n\u002F\u002F matches \"Hello, world\", \"Hello, \", but not \"Hi there\"\n```\n\nThat's the primitive. The trick is combining it with a **mapped type over `keyof T`** that recurses into each property:\n\n```ts\ntype Primitive = string | number | boolean | bigint | symbol | undefined | null | Date;\n\ntype Paths\u003CT> = T extends Primitive\n  ? never\n  : T extends readonly unknown[]\n    ? never\n    : {\n        [K in keyof T & string]: T[K] extends Primitive\n          ? K\n          : K | `${K}.${Paths\u003CT[K]>}`;\n      }[keyof T & string];\n```\n\nRead 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.\n\nGive it this `Config`:\n\n```ts\ninterface Config {\n  theme: {\n    colors: { primary: string; secondary: string };\n    darkMode: boolean;\n  };\n  name: string;\n}\n```\n\n`Paths\u003CConfig>` resolves to:\n\n```ts\n\"name\" | \"theme\" | \"theme.colors\" | \"theme.darkMode\" | \"theme.colors.primary\" | \"theme.colors.secondary\"\n```\n\nEvery 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.\n\n## Making `get` use it\n\nThe 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:\n\n```ts\ntype PathValue\u003CT, P extends string> = P extends `${infer Head}.${infer Rest}`\n  ? Head extends keyof T\n    ? PathValue\u003CT[Head], Rest>\n    : never\n  : P extends keyof T\n    ? T[P]\n    : never;\n\nfunction get\u003CT, P extends Paths\u003CT>>(obj: T, path: P): PathValue\u003CT, P> {\n  const parts = (path as string).split(\".\");\n  let current: unknown = obj;\n  for (const part of parts) current = (current as Record\u003Cstring, unknown>)?.[part];\n  return current as PathValue\u003CT, P>;\n}\n```\n\n`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.\n\nNow run the typo through it:\n\n```ts\nget(config, \"theme.colors.primary\"); \u002F\u002F string — correct, and TypeScript knows it\nget(config, \"theme.colros.primary\"); \u002F\u002F compile error, not a runtime surprise\n```\n\nThat second line doesn't run. It doesn't ship. `tsc` stops it with:\n\n```\nArgument of type '\"theme.colros.primary\"' is not assignable to parameter of type\n'\"name\" | \"theme.colors.primary\" | \"theme\" | \"theme.colors\" | \"theme.darkMode\" | \"theme.colors.secondary\"'.\n```\n\nI 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.\n\nIf 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\u003CT>` helper exists to solve exactly this problem for form fields.\n\n## Where this gets expensive\n\nTwo honest limits, because a type that silently falls back to `any` on hard cases is worse than no type at all.\n\n**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\u003CT>` 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.\n\nA 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\u003CT>` already bails out on arrays, so `Paths\u003CTreeNode>` 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.\n\n**Arrays.** The `Paths\u003CT>` 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.\n\nNeither 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.\n\n\u003C!-- playground:start -->\n\n## 🎮 Try it yourself\n\n**[▶️ Open the interactive playground →](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Ftypescript-template-literal-types\u002Fplayground)**\n\n_Runs right in your browser — poke at it and watch the concept react live._\n\n\u003C!-- playground:end -->\n\nThe 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.\n\n\u003C!-- quiz:start -->\n\n## 🧠 Test yourself\n\nThink it clicked? **[Take the 8-question quiz →](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Ftypescript-template-literal-types\u002Fquiz)**\n\n_Instant feedback, a hint on every question, and an explanation for each answer — right or wrong._\n\n\u003C!-- quiz:end -->\n\n---\n\n🚀 **Want more like this?** Every guide, playground, and quiz lives on **[bestpractic.org](https:\u002F\u002Fbestpractic.org\u002F)** — open it and **[sign up free](https:\u002F\u002Fbestpractic.org\u002F)** so the next one finds you.\n\n*Thanks for reading! Let's stay connected:*\n\n- ⭐ **GitHub** — follow me and star the projects: [github.com\u002Fparsajiravand](https:\u002F\u002Fgithub.com\u002Fparsajiravand)\n- 💬 **Discord** — join the frontend best-practices community: [discord.gg\u002Fd9KRhuAwQ](https:\u002F\u002Fdiscord.gg\u002Fd9KRhuAwQ)\n- 📸 **Instagram** — frontend best practices, daily: [@bestpractice___](https:\u002F\u002Fwww.instagram.com\u002Fbestpractice___\u002F)",{"title":111,"canonical":317,"description":112},"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Ftypescript-template-literal-types","01a1fce0-dac8-71e2-bd25-9a77d1847493",{"id":320,"locked":18},"01a1fce0-dafd-7412-8e93-e0fb87edd013",[322],{"id":45,"slug":46,"title":48,"_count":323},{"questions":51},[325],{"locale":13,"slug":46},{"id":45,"slug":46,"title":48,"_count":327,"questionCount":51},{"questions":51},[]]