我可能会这样执行:
type KeysOfValue<T, V extends T[keyof T]> =
{ [K in keyof T]-?: T[K] extends V ? K : never }[keyof T];
type PickOfValue<T, V extends T[keyof T]> = Pick<T, KeysOfValue<T, V>>
这个
KeysOfValue
类型函数使用
mapped
,
conditional
键入以拔出相关的键。
这将为您的示例生成以下结果:
type Example = PickOfValue<Response, () => Promise<any>>;
// type Example = {
// arrayBuffer: () => Promise<ArrayBuffer>;
// blob: () => Promise<Blob>;
// formData: () => Promise<FormData>;
// json: () => Promise<any>;
// text: () => Promise<string>;
// }
假设这是你想看到的,那就行了。希望有帮助,祝你好运!