Microsoft/TypeScript

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

開放

#51,376 建立於 2022年11月1日

 (1 則留言) (0 個反應) (0 位負責人)TypeScript (13,395 個分叉)batch import
BugDomain: Related Error SpansHelp Wanted

倉庫指標

星標
 (108,860 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

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.

貢獻者指南