microsoft / microsoft/TypeScript
strictNullCheck should detect unreachable default values and code after nullish operator
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
Using strictNullCheck should allow detection of code that isn't supposed to be reached according to type analysis
🔍 Search Terms
strictNullCheck unreachble nullish default value
✅ 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.
Yes, especially "Statically identify constructs that are likely to be errors."
⭐ Suggestion
This assumes that it is desired to detect errors with unreachable code due to type analysis (see microsoft/TypeScript-Website#1604 ).
With strictNullCheck turned on, some code that is meant to be unreachable and thus an error is not flagged.
📃 Motivating Example
The = 42 below is unreachable (playground) and is the sign that some code is likely to be an error, but nothing is flagged
class Example {
foo({
x = 42
}: {
x: number
}) {
return x
}
}
Either the declaration should be changed to x?: number, or the default should be removed.
Another example (thanks to @jcalz) using the nullish operator.
function foo(x: number) { x ??= 42 }
Again, it would be helpful if an error or a warning would be raised as that code seems clearly to be a mistake.
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 cách tái hiện hai ví dụ làm động lực với strictNullCheck được bật: giá trị mặc định x = 42 trong phép phân rã và x ??= 42. Xác định các chẩn đoán dự kiến cho các giá trị mặc định không thể đạt tới và các phép gán nullish, sau đó sử dụng bộ kiểm thử trình biên dịch TypeScript hiện có để xác minh cả hai trường hợp và hành vi của chúng ở trạng thái hoàn tấ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
- 32/100