Microsoft/TypeScript

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

Aberta

#51.376 aberto em 1 de nov. de 2022

 (1 comentário) (0 reação) (0 responsável)TypeScript (13.395 forks)batch import
BugDomain: Related Error SpansHelp Wanted

Métricas do repositório

Stars
 (108.860 estrelas)
Métricas de merge de PR
 (Métricas PR pendentes)

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.

Guia do colaborador