microsoft / microsoft/TypeScript

Should validate iteree when using `using`/`await using` in loop initializer

Ouverte
#60,348 0 commentaires 3 réactions 1 personne assignée Voir sur GitHub

@rbuckton y travaille déjà.

Depuis le 6/11/2024.

Needs Investigation
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

🔍 Search Terms

explicit resource management, using, await using, loop initializer, Symbol.dispose, Symbol.asyncDispose, iteration

✅ Viability Checklist
⭐ Suggestion

In this code,

export function throwsException() {
    for (using x of [1, 2, 3]) {
        console.log(x);
    }
}

I expect to see an error. It doesn't currently show one (playground link)

If I did something like using x = 3; I would get something like The initializer of a 'using' declaration must be either an object with a '[Symbol.dispose]()' method, or be 'null' or 'undefined'. I think a similar error message would be appropriate here too.

📃 Motivating Example
export function throwsException() {
    for (using x of [1, 2, 3]) {
        console.log(x);
    }
}

is preventable exception-throwing code.
And validating it a logical extension of validating

export function throwsException() {
    for (const x of [1, 2, 3]) {
        using y = x; // TS ERROR: The initializer of a 'using' declaration must be either an object with a '[Symbol.dispose]()' method, or be 'null' or 'undefined'
        console.log(x);
    }
}
💻 Use Cases
  1. What do you want to use this for? Safer usage explicit resource management features.
  2. What shortcomings exist with current approaches? No validation of explicit resource management declarations in loop initializers
  3. What workarounds are you using in the meantime? N/A.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.