microsoft / microsoft/TypeScript

add an option to report an error when `Symbol.dispose`/`Symbol.asyncDispose` objects are used without the `using` keyword

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

Chưa có ai nhận issue này.

Awaiting More Feedback Suggestion
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

### 🔍 Search Terms

dispose using error

### ✅ Viability Checklist

- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

### ⭐ Suggestion

i was surprised to learn that using disposables without the `using` keyword are not reported as an error:

```ts
const foo = {
[Symbol.dispose]: () => {
console.log("disposed"); // never called
},
};

{
const bar = foo;
console.log(bar);
}
```

### 📃 Motivating Example

[the documentation compares it to python `with` statements](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#:~:text=that%2E-,You,scope). but the whole point of context managers is to enforce that an object is always properly cleaned up. if you forget to use `with`, then the object's setup is never run so its cleanup doesn't need to run either.

since this is not the case with the `using` keyword, it should be a type error to use it in an unsafe way

### 💻 Use Cases

i have an API that i need to log out of when i'm finished using it, otherwise the session remains active

```ts
const login = () => {
// ...
return {
[Symbol.asyncDispose]: () => {
// important cleanup code that MUST be run no matter what
}
}
}
```

in python i would do this with a context manager, and i'd be confident that the cleanup code would always be executed. it's not up to the caller to remember to use the `with` statement. however in typescript, the caller could easily do this by mistake:

```ts
const sesion = await login()
// cleanup code never gets called
```

this is extremely surprising behavior coming from a language with a similar feature. is this is intentional, the docs should have a clear warning that `Symbol.dispose` and `Symbol.asyncDispose` functions have no guarantee to actually be called.

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.

Hướng nghiên cứu

Không có tệp triển khai hoặc bài kiểm thử nào được nêu. Hãy bắt đầu với tài liệu release notes TypeScript 5.2 được liên kết và các ví dụ đồng bộ và bất đồng bộ trong issue, sau đó xác định hành vi chẩn đoán và tùy chọn được dự định cho các đối tượng disposable được sử dụng mà không có using. Hoàn thành khi việc sử dụng sai được yêu cầu được báo cáo mà không thay đổi JavaScript được phát ra.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
typescript
Lĩnh vực
compilers
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
42/100

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.