microsoft / microsoft/TypeScript

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

Open
#60,583 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Proposal Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

🔎 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked TypeScript Playground reproduction and tracing diagnostic TS2367. Compare the reported '{}' and 'number' wording with the expected 'object' and 'number' wording, then add or update coverage for the shown comparison and verify that the diagnostic remains correct for the related examples.

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
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.