microsoft / microsoft/TypeScript
Comparing arrays doesn't give any error
- 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
Arrays, Compare, Less than, Greater than
### 🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about arrays and comparisons
### ⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.8.3#code/MYewdgzgLgBAhjAvDA2gIjgIzQGhm4HAEzQF0AoUSWTJVDTHYXfZiqiEAGwFMA6LiADmACgQA+ZJgCUQA
### 💻 Code
```ts
const a = [11, 12, 13]
const b = [4, 5]
console.log(a >= b)
```
### 🙁 Actual behavior
This compiles without any error, returning a potentially unexpected result at runtime.
### 🙂 Expected behavior
This should give an error. Maybe something like TS2365 "Operator '>=' cannot be applied to types 'number[]' and 'number[]'".
### Additional information about the issue
What this code actually does at runtime is that first it converts the arrays to strings, then compares the resulting strings by alphabetical order. I'm quite sure this is almost never intended/useful.
Anyone coming from languages like Python or C++ would expect the arrays to be compared lexicographically, which isn't the case here. And in my specific case I actually intended to compare the lengths of the array (and simply forgot to write `.length`), and not getting a compile time error here led to a bug in my program.
Hướng dẫn đóng góp
Hướng nghiên cứu
Bắt đầu bằng cách tái hiện hành vi trong TypeScript Playground được liên kết với phép so sánh mảng đã cung cấp. Sau đó, theo dõi cách bộ kiểm tra kiểu xử lý các toán tử quan hệ và kiểu mảng. Được xem là hoàn tất khi hành vi chẩn đoán mà issue yêu cầu được bao phủ bởi một regression test mà không làm hỏng các phép so sánh được hỗ trợ.
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
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 28/100