microsoft / microsoft/TypeScript

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

オープン
#60,348 コメント 0 件 リアクション 3 件 担当者 1 名 GitHub で見る

@rbuckton がすでに取り組んでいます。

2024年11月6日 から。

Needs Investigation
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。