microsoft / microsoft/TypeScript

Confusing error when destructuring and overwriting with a wrong type

Open
#44,165 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Error Messages Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Bug Report

I noticed it because I put the spread at the bottom instead of top, but I had a hard time finding my mistake because of this. It's easy to spot if the object is small like in this example, but in my case the error and the spread was separated by multiple lines. Any other suggested error would be much clearer, but this confused me very much.

🔎 Search Terms

destructuring, spread, error reporting

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about destructuring, spread, error reporting
⏯ Playground Link

Playground link with relevant code

💻 Code
interface Test {
  foo: boolean;
}

interface LesserTest {
  foo?: boolean | null;
}

const bar: LesserTest = {
  foo: null
}

const barWrong: Test = {
  foo: true, // Type 'boolean | null' is not assignable to type 'boolean'.
  ...bar
}

// Type '{ foo?: boolean | null | undefined; }' is not assignable to type 'Test'. - such error would be much better
const barRight: Test = {
  ...bar
}
🙁 Actual behavior

The same property will be overwritten with a wrong type, but the error is reported in a wrong place, where the type is correct.

🙂 Expected behavior

The error should be reported on the destructured variable where the type is wrong or in the main variable barWrong.

I also noticed that if those types would match we get an error 'foo' is specified more than once, so this usage will be overwritten. which is also much better.

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 with the linked TypeScript Playground example and reproduce the diagnostic for barWrong, comparing it with barRight. Trace the checker path for object spread and overwritten properties; done means the diagnostic points to the wrong-typed property or barWrong and clearly explains the resulting type mismatch.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.