microsoft / microsoft/TypeScript
Misleading error message "'{}' and 'number' have no overlap"
Nobody has claimed this yet.
- 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
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about object types and structural typing
⏯ Playground Link
💻 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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