microsoft / microsoft/TypeScript

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

Open
#58,068 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: check: Type Circularity Possible Improvement
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the failing TypeScript Playground link and compare it against the working link, using the TypeScript 5.4.3 reproduction in the issue. Trace why the spread rest parameter produces TS2310 while the array parameter does not; done means both shown examples compile successfully without the circularity error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.