Deep key paths and lookup
Concept and syntax
A bounded recursive path model can enumerate nested string keys, while a lookup helper follows template segments to compute the property type. This example supports required plain records, nondotted string keys and at most two path levels; it deliberately omits arrays and optional branches.
Working example
Save each snippet separately as main.ts in a fresh directory with TypeScript 5.8.3. Compile with tsc --strict --target ES2022 --module commonjs --moduleDetection force --skipLibCheck --noEmitOnError main.ts and run accepted snippets with node main.js. Type aliases are erased; assignments and diagnostic checks demonstrate the type relations, not the printed output alone.
type Paths<T,D extends unknown[]=[1,1]>=D extends [unknown,...infer Rest]?{[K in keyof T & string]:K|(T[K] extends object?`${K}.${Paths<T[K],Rest>}`:never)}[keyof T & string]:never; type Value<T,P extends string>=P extends `${infer H}.${infer R}`?H extends keyof T?Value<T[H],R>:never:P extends keyof T?T[P]:never; type Model={meta:{count:number};label:string}; type P=Paths<Model>;const p:P="meta.count"; const n:Value<Model,typeof p>=4; console.log(p,n);Expected output:
meta.count 4Common mistake
Intentionally invalid; compile separately and do not execute.
type Paths<T,D extends unknown[]=[1,1]>=D extends [unknown,...infer Rest]?{[K in keyof T & string]:K|(T[K] extends object?`${K}.${Paths<T[K],Rest>}`:never)}[keyof T & string]:never; type Value<T,P extends string>=P extends `${infer H}.${infer R}`?H extends keyof T?Value<T[H],R>:never:P extends keyof T?T[P]:never; type Model={meta:{count:number};label:string}; type P=Paths<Model>;const n:Value<Model,"meta.count">="four";Expected diagnostic: TS2322. The path resolves to number, not string.
Practice and recap
Add a third level and verify the path depth bound.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Deep key paths and lookup — knowledge check
Check the type transformation, trace output, diagnose a rejected example, and apply the concept to a new case.
Which statement about Deep key paths and lookup is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
type Paths<T,D extends unknown[]=[1,1]>=D extends [unknown,...infer Rest]?{[K in keyof T & string]:K|(T[K] extends object?`${K}.${Paths<T[K],Rest>}`:never)}[keyof T & string]:never; type Value<T,P extends string>=P extends `${infer H}.${infer R}`?H extends keyof T?Value<T[H],R>:never:P extends keyof T?T[P]:never; type Model={meta:{count:number};label:string}; type P=Paths<Model>;const p:P="meta.count"; const n:Value<Model,typeof p>=4; console.log(p,n);Which diagnostic and explanation match the rejected example under the lesson settings?
type Paths<T,D extends unknown[]=[1,1]>=D extends [unknown,...infer Rest]?{[K in keyof T & string]:K|(T[K] extends object?`${K}.${Paths<T[K],Rest>}`:never)}[keyof T & string]:never; type Value<T,P extends string>=P extends `${infer H}.${infer R}`?H extends keyof T?Value<T[H],R>:never:P extends keyof T?T[P]:never; type Model={meta:{count:number};label:string}; type P=Paths<Model>;const n:Value<Model,"meta.count">="four";What is Value<Model,"label">?