Last active
January 7, 2024 11:37
-
-
Save guiseek/86e5010f916225f23cafb28798828790 to your computer and use it in GitHub Desktop.
deep 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
export function deepValue<T extends object, P1 extends keyof T>( | |
value: T, | |
...path: [P1] | |
): T[P1]; | |
export function deepValue< | |
T extends object, | |
P1 extends keyof T, | |
P2 extends keyof T[P1] | |
>(value: T, ...path: [P1, P2]): T[P1][P2]; | |
export function deepValue< | |
T extends object, | |
P1 extends keyof T, | |
P2 extends keyof T[P1], | |
P3 extends keyof T[P1][P2] | |
>(value: T, ...path: [P1, P2, P3]): T[P1][P2][P3]; | |
export function deepValue< | |
T extends object, | |
P1 extends keyof T, | |
P2 extends keyof T[P1], | |
P3 extends keyof T[P1][P2], | |
P4 extends keyof T[P1][P2][P3] | |
>(value: T, ...path: [P1, P2, P3, P4]): T[P1][P2][P3][P4]; | |
export function deepValue< | |
T extends object, | |
P1 extends keyof T, | |
P2 extends keyof T[P1], | |
P3 extends keyof T[P1][P2], | |
P4 extends keyof T[P1][P2][P3], | |
P5 extends keyof T[P1][P2][P3][P4] | |
>(value: T, ...path: [P1, P2, P3, P4, P5]): T[P1][P2][P3][P4][P5]; | |
export function deepValue< | |
T extends object, | |
P1 extends keyof T, | |
P2 extends keyof T[P1], | |
P3 extends keyof T[P1][P2], | |
P4 extends keyof T[P1][P2][P3], | |
P5 extends keyof T[P1][P2][P3][P4], | |
P6 extends keyof T[P1][P2][P3][P4][P5] | |
>(value: T, ...path: [P1, P2, P3, P4, P5, P6]): T[P1][P2][P3][P4][P5][P6]; | |
export function deepValue< | |
T extends object, | |
P1 extends keyof T, | |
P2 extends keyof T[P1], | |
P3 extends keyof T[P1][P2], | |
P4 extends keyof T[P1][P2][P3], | |
P5 extends keyof T[P1][P2][P3][P4], | |
P6 extends keyof T[P1][P2][P3][P4][P5], | |
P7 extends keyof T[P1][P2][P3][P4][P5][P6] | |
>( | |
value: T, | |
...path: [P1, P2, P3, P4, P5, P6, P7] | |
): T[P1][P2][P3][P4][P5][P6][P7]; | |
export function deepValue< | |
T extends object, | |
P1 extends keyof T, | |
P2 extends keyof T[P1], | |
P3 extends keyof T[P1][P2], | |
P4 extends keyof T[P1][P2][P3], | |
P5 extends keyof T[P1][P2][P3][P4], | |
P6 extends keyof T[P1][P2][P3][P4][P5], | |
P7 extends keyof T[P1][P2][P3][P4][P5][P6], | |
P8 extends keyof T[P1][P2][P3][P4][P5][P6][P7] | |
>( | |
value: T, | |
...path: [P1, P2, P3, P4, P5, P6, P7, P8] | |
): T[P1][P2][P3][P4][P5][P6][P7][P8]; | |
export function deepValue<T, P>(value: T, ...path: P[]) { | |
for (const key of path.join().split('.')) value = value[key]; | |
return value; | |
} | |
deepValue({ a: { b: '10' }, x: { y: 10 } }, 'a', 'b'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment