Skip to content

Instantly share code, notes, and snippets.

@boopathi
Last active March 4, 2020 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boopathi/cddcf92c207e8adcf5df7224dc1b1533 to your computer and use it in GitHub Desktop.
Save boopathi/cddcf92c207e8adcf5df7224dc1b1533 to your computer and use it in GitHub Desktop.
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