microsoft / microsoft/TypeScript
Allow "catch (e as Error)" instead of "catch (e: unknown)"
Chưa có ai nhận issue này.
- 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:
- In a professional code base, thrown objects always implement the
Errorinterface. 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. - It's wasteful for every single
catchblock to perform paranoid runtime tests forinstanceof Error. - The TypeScript compiler doesn't even support
instanceof Errorfor transpiled code. - Relaxing this rule won't cause any trouble; if some people really prefer
unknownthey 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
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- 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