microsoft / microsoft/TypeScript
Ensure forward-compatibility of callbacks
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
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/
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con los ejemplos de requestAnimationFrame y revisa los TypeScript Design Goals enlazados y el comportamiento de los argumentos de callback. Compara la solución alternativa del parámetro never con la function-type assertion propuesta; el trabajo estaría terminado cuando exista un diseño acordado que preserve el comportamiento actual en tiempo de ejecución y especifique cómo se crean y comprueban las declaraciones de la biblioteca.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript, typescript
- Área
- compilers, tooling
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 32/100