Skip to content

Generic function with function as argument autocomplete broken, unless generic argument of the argument function is it's first argument #60648

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

Open
julijan opened this issue Dec 1, 2024 · 1 comment · May be fixed by #56460
Labels
Experience Enhancement Noncontroversial enhancements Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@julijan
Copy link

julijan commented Dec 1, 2024

🔎 Search Terms

typescript generic function no autocomplete function as argument

🕗 Version & Regression Information

Relevant versions are 5.4.5 and above, tested in all versions from 5.4.5 to 5.8.0 (Nightly) inclusive, I was able to reproduce the bug in all versions.

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.2#code/IYZwngdgxgBAZgV2gFwJYHsIwE4FMDmqIyu2ACgDYKEQBCwAJgDwDyADsjLgB4kQMgYAJVxR02ZsWyoI+ADQxgEMAD4VACijAKFAEbAoAawBcMdcDZtTSsAvQcMEEKfbIAlDAC8KmADd0qAx2HM4wAHLoAJIQcKSsHCpupmTY6AC2RLhM-oE+AN4AvgBQRXiExKSU1DL0DOrmlsHIoXkwIOm4yAAWMvimUr0wBR7eMIUKeUUwMAD0MzC6uBToAO6KCMjoYmlsFJ24MEQLqYa4EAq4vmcw3ejUXTddB8hgbAdHbKlv2BRgftqBYAkBhTNodIrDADcJSKc0OEBIeGIvV+CmAGy26V2+xgK3EhkEqDgMAARPY0JgQCTFNh8Ag0mdONtcII4KhsMQFHBxFxuMAdntjEVQJBYIgUI4cAQiIiqjQAOLodDMVy8vgCYSicSSZDSWRo5RqTTaPQGExmcmOUKuNGWazKEY+HJBGDk0IRaKxbDxZCJZKpDIgLLO-LFUrSirkKgKpV1dRu0ytdoM7q9fq6wYFW1WRQOryhiaguGLZZrdGbbbYki4-EgUHJ3AQtyQoA

💻 Code

async function registerPluginBad<Opt extends Record<string, any>>(callback: (app: any, options: Opt) => void, opts: NoInfer<Opt>): Promise<void> {}

registerPluginBad((app, opts: { something: string }) => {}, {
  // below autocomplete is broken, even though the type is properly validated
  some
});


// interestingly, autocomplete works if "options" argument comes first, for example:
async function registerPluginGood<Opt extends Record<string, any>>(callback: (options: Opt, app: any) => void, opts: NoInfer<Opt>): Promise<void> {}

registerPluginGood((opts: { something: string }, app: any) => {}, {
  // below autocomplete works
  some
});

🙁 Actual behavior

This one has to be tested in the playground:
registerPluginBad - second argument does not autocomplete, however type is properly validated
registerPluginGood - exact same function, except the first argument "callback" arguments have changed the order, which for some reason fixes the autocomplete

🙂 Expected behavior

For both functions, autocomplete should work. Argument order should not matter.
Both do not infer from "opts", so any inference should be from "options" argument of the callback.

Additional information about the issue

No response

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Help Wanted You can do this Experience Enhancement Noncontroversial enhancements labels Dec 2, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Dec 2, 2024
@Andarist
Copy link
Contributor

Andarist commented Dec 2, 2024

The problem here is not that the order of the parameters and arguments is different. Your registerPluginGood call annotates both parameters of the callback. This makes it context-insensitive, and it can, thanks to that, hit different inference paths.

It happens that one of my old PRs addresses this: #56460

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
4 participants
@julijan @RyanCavanaugh @Andarist and others