microsoft / microsoft/TypeScript
Ensure forward-compatibility of callbacks
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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/
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con gli esempi di requestAnimationFrame e rivedi i TypeScript Design Goals collegati e il comportamento degli argomenti di callback. Confronta il workaround con parametro never con la function-type assertion proposta; il lavoro sarebbe completo quando esistesse un design concordato che preservi il comportamento runtime esistente e specifichi come vengono create e verificate le dichiarazioni della libreria.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, typescript
- Ambito
- compilers, tooling
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 32/100