microsoft / microsoft/TypeScript

Allow "catch (e as Error)" instead of "catch (e: unknown)"

Đang mở
#42,596 10 bình luận 51 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ả

Suggestion

TypeScript 3.x forbids type annotations in catch blocks:

try {
  // ...
} catch (e: Error) { // <-- TS1996 "Catch clause variable cannot have a type annotation"
  // ...
}

But TypeScript 4.x relaxed this rule to accept any or unknown only:

try {
  // ...
} catch (e: Error) { // <-- TS1996 "Catch clause variable type annotation must be 'any' or 'unknown' if specified"
  // ...
}

Suggestion: Could we relax the rule to allow Error as well?

Such a declaration would not accurately describe pathological JavaScript code.

But here's why it makes sense anyway:

  1. In a professional code base, thrown objects always implement the Error interface. We have lint rules that enforce this. And external packages generally follow this rule as well, at least the kind we'd use for professional work.
  2. It's wasteful for every single catch block to perform paranoid runtime tests for instanceof Error.
  3. The TypeScript compiler doesn't even support instanceof Error for transpiled code.
  4. Relaxing this rule won't cause any trouble; if some people really prefer unknown they can enable it via a lint rule like no-implicit-any-catch without any involvement from the compiler.
Alternate syntax

In the thread below, @MickeyPhoenix suggested to use as instead of : to clarify that technically this is a type cast, while still keeping the syntax concise and intuitive:

try {
  // ...
} catch (e as Error) {
  // ...
}

🔍 Search Terms

catch instanceof TS1996 1996

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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

Bắt đầu bằng việc xem xét cách compiler xử lý các chú thích kiểu trong các mệnh đề catch và chẩn đoán TS1996, sau đó xem lại phần thảo luận trong thread của issue về ':' so với 'as'. Hoàn tất sẽ yêu cầu một cú pháp được thống nhất và hành vi kiểm tra kiểu tương ứng, cùng với các bài kiểm thử bao quát khai báo catch được đề xuất.

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
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/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.