microsoft / microsoft/TypeScript

Comparing arrays doesn't give any error

Aperta
#61,889 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo il comportamento nel TypeScript Playground collegato con il confronto tra array fornito. Poi segui il modo in cui il type checker gestisce gli operatori relazionali e i tipi array. Il lavoro è completato quando il comportamento diagnostico richiesto dall'issue è coperto da un test di regressione senza interrompere i confronti supportati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
28/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.