microsoft / microsoft/TypeScript

Ensure forward-compatibility of callbacks

Offen
#42,579 15 Kommentare 48 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

Suggestion

🔍 Search Terms

strict callback arguments, too many arguments warning, option

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Edit: My original suggestion was unworkable. This is a second attempt.

const nextFrame = new Promise(requestAnimationFrame);

The above results in requestAnimationFrame being called with two arguments rather than the one it accepts. This is fragile, since, in future, a second argument may be added to requestAnimationFrame which makes the above code behave differently.

This problem is worse when it comes to browser-provided functions, as those change with new browser versions, and don't rely on an app redeploy.

This can be worked around using never:

declare function requestAnimationFrame(callback: FrameRequestCallback, _: never): number;

…but it makes for a messy auto-complete, as the 'never' arg shows up.

A solution would be some way to mark a function as "cannot be assigned to a function type with more parameters". This would allow library authors to make this assertion if they want to reserve additional params for future use, and this assertion could be added to DOM functions like requestAnimationFrame.

💻 Use Cases

I wrote a blog post about the risks with this pattern, including a section on how TypeScript doesn't prevent it https://jakearchibald.com/2021/function-callback-risks/

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

Beginnen Sie mit den requestAnimationFrame-Beispielen und prüfen Sie die verlinkten TypeScript Design Goals sowie das Verhalten der callback-Argumente. Vergleichen Sie den never-Parameter-Workaround mit der vorgeschlagenen function-type assertion; abgeschlossen wäre die Arbeit erst, wenn ein abgestimmtes Design vorliegt, das das bestehende Laufzeitverhalten beibehält und festlegt, wie Bibliotheksdeklarationen erstellt und geprüft werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers, tooling
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
32/100

Neue Issues direkt in Ihr Postfach

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