microsoft / microsoft/TypeScript

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

Offen
#60,648 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Experience Enhancement Help Wanted Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Öffne das verknüpfte TypeScript Playground und reproduziere den Unterschied zwischen registerPluginBad und registerPluginGood. Verfolge das Verhalten der Autovervollständigung und der generischen Inferenz in Bezug auf die Reihenfolge der Callback-Parameter; abgeschlossen ist die Aufgabe, wenn beide Aufrufe für das Optionsobjekt eine Vervollständigung anbieten und gleichzeitig die Typvalidierung erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers, developer-experience
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.