Last active
March 4, 2020 21:24
-
-
Save boopathi/cddcf92c207e8adcf5df7224dc1b1533 to your computer and use it in GitHub Desktop.
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 ObjectPathNormalize = T extends Array<infer U> | |
? U extends object | |
? Required<U> | |
: never | |
: T extends object | |
? Required<T> | |
: never; | |
type Normalize<T> = ObjectPathNormalize<T>; | |
type Normalize1<T, Key1 extends keyof Normalize<T>> = ObjectPathNormalize< | |
Normalize<T>[Key1] | |
>; | |
type Normalize2< | |
T, | |
Key1 extends keyof Normalize<T>, | |
Key2 extends keyof Normalize1<T, Key1> | |
> = ObjectPathNormalize<Normalize1<T, Key1>[Key2]>; | |
type Normalize3< | |
T, | |
Key1 extends keyof Normalize<T>, | |
Key2 extends keyof Normalize1<T, Key1>, | |
Key3 extends keyof Normalize2<T, Key1, Key2> | |
> = ObjectPathNormalize<Normalize2<T, Key1, Key2>[Key3]>; | |
function createObjectPath<T>(_: T) { | |
return function field< | |
Key1 extends keyof Normalize<T>, | |
Key2 extends keyof Normalize1<T, Key1>, | |
Key3 extends keyof Normalize2<T, Key1, Key2>, | |
Key4 extends keyof Normalize3<T, Key1, Key2, Key3> | |
>(k1: Key1, k2?: Key2, k3?: Key3, k4?: Key4) { | |
if (k2) { | |
if (k3) { | |
if (k4) { | |
return [k1, k2, k3, k4]; | |
} | |
return [k1, k2, k3]; | |
} | |
return [k1, k2]; | |
} | |
return [k1]; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment