microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.32k stars 12.53k forks source link

Too wide diagnostic with array spread #58110

Open guillaumebrunerie opened 7 months ago

guillaumebrunerie commented 7 months ago

🔎 Search Terms

array spread, diagnostics

🕗 Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.4#code/C4TwDgpgBAggRgYygXigIgIZqgH3XbPNBNAbgChyEB7AOwGdgoMAnFjEARgC5ZEBtALooo-TGgA0+NIIpU6jZmw4AmXvARCR-clD1QAdEdbsuE3fvFSLetAXP70AE0nlBQA

💻 Code

type Abc = "a" | "b" | "c";

const array1: Abc[] = ["a", "b"];

const array2: Abc[] = [
    ...array1,
    "a", 
    "b",
    "d",
]

🙁 Actual behavior

The whole contents of array2 is highlighted in red.

🙂 Expected behavior

Only the "d" should be highlighted in red, as it is the only incorrect part of array2. Note that if we remove the spread ...array1, then it works as expected.

Additional information about the issue

With more complicated types/data, this bug can make it pretty hard to diagnose where the problem is.

Andarist commented 7 months ago

This is already fixed here: https://github.com/microsoft/TypeScript/pull/52850 . I just pushed out a new test case based on this issue: https://github.com/microsoft/TypeScript/pull/52850/commits/76d7f4e286463b316d8c36da5fa8b9821a228c59