See code below StackBlitz
I can’t figure out how to make typescript infer the return type correctly. It’s supposed to be an object with a results property, but instead, it’s never
.
You can tell by the error in the 2nd argument that it’s able to infer the parameters just fine, or it wouldn’t complain about the string argument where a number is expected.
import { Client } from '@hubspot/api-client';
type GetReturnType<T> = T extends (...args: unknown[]) => Promise<infer U> ? U : never;
type GetParameters<T> = T extends (...args: infer U) => unknown ? U : never;
async function call<T extends 'crm', U extends keyof Client[T], V extends keyof Client[T][U], W extends keyof Client[T][U][V]>(
method: [api: T, entity: U, apiSection: V, method: W],
...args: GetParameters<Client[T][U][V][W]>
): Promise<GetReturnType<Client[T][U][V][W]>> {
[method, args]
return undefined as any
}
const res = await call(
['crm', 'companies', 'basicApi', 'getPage'],
'test', // correctly shows error because it should be number
undefined,
['city'],
);
res.results; // res is `never`. I don't understand why.