microsoft / microsoft/TypeScript
Array prototype extensions fail circularly with `...args: T[]`, but not `args: T[]`
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 Search Terms
Typescript array prototype extension spread rest variadic parameter circular failure TS2310 version 5.4.3
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about circular compilation failures
I've added two playground links; the first works, the second fails. The only difference between the snippets is receiving an array T[] as a spread argument vs a single argument.
⏯ Playground Link
💻 Code
Failing:
const newMethods = {
add<T>(this: T[], ...values: T[]): T { this.push(...values); return values[0]; }
};
type NewArrayMethods = typeof newMethods;
interface Array<T> extends NewArrayMethods {};
Object.defineProperty(Array.prototype, 'add', { enumerable: false, writable: true, value: newMethods.add });
const myArr: string[] = [];
myArr.add('abc', 'def', 'ghi');
console.log({ myArr });
Working (only difference is ...values: T[] is now values: T[] - without ...):
const newMethods = {
add<T>(this: T[], values: T[]): T { this.push(...values); return values[0]; }
};
type NewArrayMethods = typeof newMethods;
interface Array<T> extends NewArrayMethods {};
Object.defineProperty(Array.prototype, 'add', { enumerable: false, writable: true, value: newMethods.add });
const myArr: string[] = [];
myArr.add([ 'abc', 'def', 'ghi' ]);
console.log({ myArr });
🙁 Actual behavior
Example using ...values: T[] fails to compile!
🙂 Expected behavior
Both examples should compile successfully.
Additional information about the issue
No response
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia dal link di TypeScript Playground che fallisce e confrontalo con il link funzionante, usando la riproduzione con TypeScript 5.4.3 presente nell’issue. Traccia il motivo per cui il parametro rest con spread produce TS2310 mentre il parametro array non lo fa; il lavoro è completato quando entrambi gli esempi mostrati vengono compilati correttamente senza l’errore di circolarità.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 30/100