Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Difference in parameter inference between arrow functions and classic functions #50258

Closed
knutkj opened this issue Aug 10, 2022 · 3 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@knutkj
Copy link

knutkj commented Aug 10, 2022

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
Copy link
Author

knutkj commented Aug 10, 2022

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

image

@RyanCavanaugh
Copy link
Member

Duplicate #47599

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 10, 2022
@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants