microsoft / microsoft/TypeScript

Array prototype extensions fail circularly with `...args: T[]`, but not `args: T[]`

Aperta
#58,068 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: check: Type Circularity Possible Improvement
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

Working: https://www.typescriptlang.org/play?#code/PTAEBUAsEsGdQO4HsBOBrWAuAUAYyQHawAuoBApggLLnGRIAm8AvKAN7ahegCGDDAHnAA+ABR04mCAG0AugBpQANx4AbAK7ksM2QEop4dqAmwAdAAd1sSKNN2VGrboDcoFLXUoCytZtjSABllXAF9sEOdsYgBPc3JQADlKAEEUFB5omjpGFmNY8iQAMzJKLPomSOgCYnIUQp5ceNT06KFhUHIADxqCJkSUtIyynPYI7GwAeQAjACtyXGJTBnJCqvIABRQkOJQY0WaMiy3iJBi4xQByPgYLxTYOgnUAW1qeKdVyKXrVWHJFBBQ0GIbw+UmIKE0igcmikFGotHKZmuoBCLnG+CIpCe0WaUhIgIIAHM5KBWHJItjmqZrqJpKArlNcLd6ctCsyLoSYBdQHpInhCLAkB9TKokITRPdKWkUS4gA

Failing: https://www.typescriptlang.org/play?#code/PTAEBUAsEsGdQGYENoBtYC4BQBjA9gHawAuoBApgO4Cy5xkeAJvALygDeWo3oSjjAHnAA+ABT04GCAG0AugBpQAOhUA3JKgCu5TDNkBKKeA6gJsJQAdNsSKJVL1WnfoDcoAE51N7gqEfbYaQAGWTcAXywwlyxiAE8LclAAOSoAQXd3JFjaeiZWU3jyPAQyKhyGZmjoAmJyd2QcRPTM2KFhUHIAD1qCZmS0jKzyvI4orCwAeQAjACtyHGIlRnIEavIABXc8BPc40Wasyy3iPDiExQByPkYLxXYOgk0AWzqkKdRyKWR0ckVKd2gxDeHykxHc2kU-k+pRodAq5muoDCrnG+CIpCesWaUhIAIIAHM5KA2HJopjmkprqIrlMcLdQBdlgh6Rd8TALijcIRYHgPkpUHh8aJ7uSMkjXEA

💻 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

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. 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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.