Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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