Is there a way to create a type that selects only properties from an object whose values match a specific type? For example:
type PickOfValue<T, V extends T[keyof T]> = {
[P in keyof (key-picking magic?)]: T[P];
};
I am looking for a solution where I can choose keys (properties) of T
that have values matching the type V
(T[P] extends V
is true). I haven't been able to figure this out on my own, so any assistance would be greatly appreciated.
Here is an example output:
PickOfValue<Response, () => Promise<any>>; // {json: () => Promise<any>, formData: () => Promise<FormData>, ...}
PickOfValue<{a: string | number, b: string, c: number, d: "", e: 0}, string | number>; // {a: string | number, b: string, c: number, d: "", e: 0}