microsoft / microsoft/TypeScript

Confusing error when destructuring and overwriting with a wrong type

Offen
#44,165 4 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: Error Messages Help Wanted
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem verlinkten TypeScript Playground-Beispiel und reproduziere die Diagnose für barWrong, indem du sie mit barRight vergleichst. Verfolge den Checker-Pfad für Object Spread und überschriebene Eigenschaften; als abgeschlossen gilt die Aufgabe, wenn die Diagnose auf die Eigenschaft mit dem falschen Typ oder auf barWrong zeigt und die daraus resultierende Typinkongruenz eindeutig erklärt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.