Skip to content

Instantly share code, notes, and snippets.

@samkcarlile
Created August 29, 2025 06:01
Show Gist options
  • Select an option

  • Save samkcarlile/c490bce8951696a4759b1528f7222442 to your computer and use it in GitHub Desktop.

Select an option

Save samkcarlile/c490bce8951696a4759b1528f7222442 to your computer and use it in GitHub Desktop.
Typed Object Path Value
type ObjectPathValue<T, U extends readonly string[]> = U extends [
infer FirstProp extends keyof T,
...infer RestProps extends readonly string[]
]
? ObjectPathValue<T[FirstProp], RestProps>
: T;
function $get<T extends object, U extends readonly string[]>(
target: T,
path: [...U]
): ObjectPathValue<T, U> {
for (let i = 0; i < path.length - 1; i++) target = (target as any)[path[i]];
return (target as any)[path[path.length - 1]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment