microsoft / microsoft/TypeScript

Comparing arrays doesn't give any error

未关闭
#61,889 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
Awaiting More Feedback Suggestion
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

### 🔎 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.

贡献指南

打开贡献指南

调研方向

首先,使用提供的数组比较在链接的 TypeScript Playground 中重现该行为。然后跟踪类型检查器如何处理关系运算符和数组类型。完成的标准是:issue 请求的诊断行为已由回归测试覆盖,同时不会破坏受支持的比较。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
28/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。