microsoft / microsoft/TypeScript

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

Đang mở
#60,348 0 bình luận 3 reaction 1 người được giao Xem trên GitHub

@rbuckton đang làm issue này rồi.

Từ ngày 6/11/2024.

Needs Investigation
Ngôn ngữ chính
Go
Star
111k
Fork
14.4k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
117

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.