microsoft / microsoft/TypeScript
Provide a way to type a fix-length tuple-like generator/iterator
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
Search Terms
- generator
- iterator
Suggestion
Express a fixed-length generator.
Use Cases
- Allows the spread syntax on functions to know how many arguments is deconstructed (see examples below)
Examples
// Vector2.ts
class Vector2 {
constructor (public x, public y) {
this.x = x;
this.y = y;
}
//... A bunch of vector methods
public* [Symbol.iterator]: Generator<number, void, unknown> {
yield this.x;
yield this.y;
}
}
// main.js
const canvas = document.querySelector('#canvas');
const ctx = canvas.getContext('2d');
const a = new Vector2(0, 0);
const b = new Vector2(10, 10);
ctx.beginPath();
ctx.moveTo(...a);
// works correctly, but with this warning:
// Expected 2 arguments, but got 0 or more.ts(2556)
ctx.lineTo(...b);
ctx.stroke();
ctx.closePath();
Right now, the code above produces the correct behavior. However, typescript doesn't not recognize how many arguments is passed to ctx.moveTo and ctx.lineTo therefore it warns me about it.
My Implementation
There could be an tuple-like syntax for generators as well, for example:
public* [Symbol.iterator]: *[number, number] {
yield this.x;
yield this.y;
}
With this syntax, typescript recognizes how many argument is deconstructed by the spread operator, and knows its a generator with the prepending *.
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, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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 reproduciendo el ejemplo de Vector2 e inspecciona cómo TypeScript tipa Generator, Symbol.iterator, los tipos similares a tuplas y los argumentos spread. Se considera terminado cuando un generador o iterador de longitud fija puede expresar su aridad, de modo que al pasarlo mediante spread a una función con parámetros fijos se compruebe sin la advertencia actual, mientras el comportamiento existente de JavaScript permanece sin cambios.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript, typescript
- Área
- compilers
- 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
- 25/100