microsoft / microsoft/TypeScript

Comparing arrays doesn't give any error

Open
#61,889 4 comments 0 reactions 0 assignees View on GitHub
Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the behavior in the linked TypeScript Playground with the provided array comparison. Then trace how the type checker handles relational operators and array types. Done means the issue's requested diagnostic behavior is covered by a regression test without breaking supported comparisons.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.