Is there a way to modify a function to return its value based on the type of the argument as a tuple? For instance, I need to adjust the getAll
function so that it returns values based on an argument provided in a tuple.
type PathImpl<T, Key extends keyof T> =
Key extends string
? T[Key] extends Record<string, any>
? | `${Key}.${PathImpl<T[Key], Exclude<keyof T[Key], keyof any[]>> & string}`
| `${Key}.${Exclude<keyof T[Key], keyof any[]> & string}`
: never
: never;
type PathImpl2<T> = PathImpl<T, keyof T> | keyof T;
type Path<T> = PathImpl2<T> extends string | keyof T ? PathImpl2<T> : keyof T;
type PathValue<T, P extends Path<T>> =
P extends `${infer Key}.${infer Rest}`
? Key extends keyof T
? Rest extends Path<T[Key]>
? PathValue<T[Key], Rest>
: never
: never
: P extends keyof T
? T[P]
: never;
declare function get<T, P extends Path<T>>(obj: T, path: P): PathValue<T, P>;
const object = {
firstName: "test",
lastName: "test1"
} as const;
get(object, "firstName");
declare function getAll<T, P extends Path<T>>(obj: T, args: P[]): PathValue<T, P>;
const data = getAll(object, ['firstName', 'lastName'])
// how to produce the type: ['test', 'test1']