microsoft / microsoft/TypeScript
Incorrectly reported `TS2322` error on array items after spread
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 Search Terms
"array spread", "TS2322"
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about
⏯ Playground Link
💻 Code
const a: Array<number | string> = [];
const b: Array<number> = [...a, 1];
🙁 Actual behavior
The compiler gives the following two errors, of which only one is appropriate:
~/prog/typescript ❯ tsc ./array-spread-error.ts
array-spread-error.ts:2:27 - error TS2322: Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
2 const b: Array<number> = [...a, 1];
~~~~
array-spread-error.ts:2:33 - error TS2322: Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
2 const b: Array<number> = [...a, 1];
~
🙂 Expected behavior
The spread is clearly incorrect, as the original array could potentially contain strings as well, but the number literal should not be marked with the same error.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der verknüpften TypeScript Playground-Reproduktion und bestätige, dass der Spread und das darauf folgende numerische Literal separate TS2322-Diagnosen erzeugen. Verfolge den Checker-Pfad für Array-Literale mit Spread-Elementen und füge einen Regressionstest hinzu, der zeigt, dass nur der ungültige Spread gemeldet wird, während das numerische Literal akzeptiert wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 42/100