microsoft / microsoft/TypeScript

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

Offen
#60,348 0 Kommentare 3 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

@rbuckton arbeitet bereits daran.

Seit 06.11.2024.

Needs Investigation
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔍 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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.