microsoft / microsoft/TypeScript
Provide a way to type a fix-length tuple-like generator/iterator
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par reproduire l’exemple Vector2 et examinez comment TypeScript type Generator, Symbol.iterator, les types semblables à des tuples et les arguments spread. Le travail est terminé lorsqu’un générateur ou un itérateur de longueur fixe peut exprimer son arité, de sorte que sa transmission par spread à une fonction à paramètres fixes soit vérifiée sans l’avertissement actuel, tout en laissant inchangé le comportement JavaScript existant.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- javascript, typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 25/100