microsoft / microsoft/TypeScript

Comparing arrays doesn't give any error

オープン
#61,889 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、リンクされた TypeScript Playground で、提供された配列比較を使って挙動を再現します。次に、型チェッカーが関係演算子と配列型をどのように処理するかを追跡します。完了とは、サポートされている比較を壊すことなく、issue が要求する診断動作が回帰テストでカバーされていることを意味します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
28/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。