Microsoft/TypeScript

Spread operator with wrong optional property raises error on incorrect source line

Open

#51.376 geöffnet am 1. Nov. 2022

Auf GitHub ansehen
 (1 Kommentar) (0 Reaktionen) (0 zugewiesene Personen)TypeScript (6.726 Forks)batch import
BugDomain: Related Error SpansHelp Wanted

Repository-Metriken

Stars
 (48.455 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)

Beschreibung

Bug Report

🔎 Search Terms

spread error wrong line

🕗 Version & Regression Information

  • This is the behavior in every version I tried, [not applied: and I reviewed the FAQ for entries about]. I tried on v3.3.3 and also on nightly.

⏯ Playground Link

Playground link with relevant code

💻 Code

type Item = {
    attribute: string;
};

const result: Partial<Record<'attribute', string | null>> = {
    attribute: "",
}

const item: Item ={
    attribute: "",
    ...result,
}

🙁 Actual behavior

This code is, of course, an error. The surprising thing is where the error is raised. In this case the line reading attribute: "", will be highlighted with Type 'string | null' is not assignable to type 'string'.

In this simple reproduction the error is clear, but when I discovered it the types were more complicated and there were a dozen or more attributes in between the scary red line and the offending spread.

🙂 Expected behavior

The diagnostic error would be much more helpful if the spread containing the invalid property was highlighted instead of the property which actually has correct types.

Contributor Guide