Reputation: 19248
How to return the correct type associated function argument as a tuple? I would like a function to return its value based on its argument's tuple type.
The example below is a get
function, but I would like to get it working with getAll
function and argument as 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']
Upvotes: 0
Views: 100
Reputation: 249556
You can use a mapped type to map the tuple passed in. You also have to change the type parameter to capture the tuple type.
type MapAllPaths<T, P extends Path<T>[]> = {} & {
[K in keyof P ]: PathValue<T, P[K] & Path<T>>
}
declare function getAll<T, P extends [Path<T>] | Path<T>[]>(obj: T, args: P): MapAllPaths<T, P>;
Upvotes: 1