microsoft / microsoft/TypeScript

Comparing arrays doesn't give any error

Ouverte
#61,889 4 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Awaiting More Feedback Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
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.

Guide de contribution

Ouvrir le guide de contribution

Piste de recherche

Commencez par reproduire le comportement dans le TypeScript Playground lié avec la comparaison de tableaux fournie. Suivez ensuite la manière dont le vérificateur de types traite les opérateurs relationnels et les types de tableaux. Le travail est considéré comme terminé lorsque le comportement de diagnostic demandé par l'issue est couvert par un test de régression sans casser les comparaisons prises en charge.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Clairement spécifiée
Accessibilité débutants
28/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.