microsoft / microsoft/TypeScript
Nullish coalescing should always include the type of the right operand
Chưa có ai nhận issue này.
- 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ả
$ ./node_modules/.bin/tsc --version
Version 3.7.2
Search Terms:
Code
A toy example would be something like this.
let foo: string = "";
foo = "bar" ?? 123;
This one is obviously fine since "bar" is always truthy.
However, this becomes a little bit problematic when you consider the idiom of having Record objects and checking their truthiness before using them.
const elts = ["foo", "bar", "spam", "spam", "foo", "eggs"];
const counts: Record<string, number>;
for (const elt of elts) {
// This really **should** raise an error.
counts[elt] = counts[elt] ?? "zero";
counts[elt] += 1;
}
Expected behavior:
An error should be raised.
Actual behavior:
Curiously, an error is not raised in strict mode but is raised in un-strict mode.
$ ./node_modules/.bin/tsc ./foo.ts
foo.ts:5:3 - error TS2322: Type 'number | "zero"' is not assignable to type 'number'.
Type '"zero"' is not assignable to type 'number'.
5 counts[elt] = counts[elt] ?? "zero";
~~~~~~~~~~~
Found 1 error.
$ ./node_modules/.bin/tsc --strict ./foo.ts
# No error, exits 0 and emits JS.
Playground Link:
Playground Link
Toggling the strictNullChecks config option will show the issue.
Related Issues:
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
Tái hiện các ví dụ với TypeScript 3.7.2 bằng cả lệnh biên dịch mặc định và --strict, sau đó so sánh các chẩn đoán đối với phép gán nullish-coalescing. Công việc hoàn tất khi biểu thức được báo cáo luôn bao gồm kiểu của toán hạng bên phải và phát sinh lỗi mong đợi trong cấu hình đã nêu.
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
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- 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
- 45/100