microsoft / microsoft/TypeScript

Misleading error message "'{}' and 'number' have no overlap"

オープン
#60,583 コメント 2 件 リアクション 1 件 担当者 0 名 GitHub で見る

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

Needs Proposal Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

🔎 Search Terms

empty object type, unintentional comparison

🕗 Version & Regression Information
⏯ Playground Link

https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&target=99&ts=5.8.0-dev.20241124#code/JYMwBAFA3gvmC8iwAYCUZZA

💻 Code
if ({} === 0) {}
🙁 Actual behavior

TS 2367: "This comparison appears to be unintentional because the types '{}' and 'number' have no overlap."

🙂 Expected behavior

TS 2367: "This comparison appears to be unintentional because the types 'object' and 'number' have no overlap."

Additional information about the issue

the type {} and number do have overlap; number is a subset of {}.

declare const n: number;
// allowed!
const emptyObjectType: {} = n;

TS is correct to report an error, but it should give a technically correct reason for the error.

Analogous (very contrived) examples can be made for similar coincidentally-including-primitive types, like

// This comparison appears to be unintentional because the types '{ toFixed(): string; toPrecision(): string; }' and 'number' have no overlap.
if ({ toFixed(): string { return "" }; toPrecision(): string { return "" }} === 0) {}

// yet this is allowed
const numbery: { toFixed(): string; toPrecision(): string; } = 0;

Stems from discord discussion beginning here: https://discord.com/channels/508357248330760243/508357638677856287/1310419206260654081

Closely related to discussion regarding https://github.com/microsoft/TypeScript/issues/60582

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

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

はじめの一歩

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

調査の方向性

まず、リンクされた TypeScript Playground の再現コードを実行し、診断 TS2367 を追跡します。報告された '{}' と 'number' という文言を、期待される 'object' と 'number' という文言と比較し、次に、示されている比較のカバレッジを追加または更新して、関連する例でも診断が正しいままであることを確認します。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

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

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