TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Difference in parameter inference between arrow functions and classic functions

Open knutkj opened this issue 3 years ago • 2 comments

In the example code below the arrow function has correct type inference while the function example is unable to infer the parameter type and falls back to any.

declare function monitor<T extends (...args: any) => any>(
  extractor: (...args: Parameters<T>) => Record<string, unknown>,
  executor: T,
): (...args: Parameters<T>) => ReturnType<T>;

monitor((p) => ({ p }), (p: number) => p);
monitor((p) => ({ p }), function (p: number) { return p; });

image

image

knutkj avatar Aug 10 '22 13:08 knutkj

Note that if we specify type of this (this: void) the function variant is also able to infer the type:

image

knutkj avatar Aug 10 '22 13:08 knutkj

Duplicate #47599

RyanCavanaugh avatar Aug 10 '22 19:08 RyanCavanaugh

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

typescript-bot avatar Aug 13 '22 18:08 typescript-bot