Microsoft/TypeScript

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

Open

#51,376 opened on Nov 1, 2022

View on GitHub
 (1 comment) (0 reactions) (0 assignees)TypeScript (48,455 stars) (6,726 forks)batch import
BugDomain: Related Error SpansHelp Wanted

Description

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