Created
August 29, 2025 06:01
-
-
Save samkcarlile/c490bce8951696a4759b1528f7222442 to your computer and use it in GitHub Desktop.
Typed Object Path Value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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