microsoft / microsoft/TypeScript
Array prototype extensions fail circularly with `...args: T[]`, but not `args: T[]`
Nobody has claimed this yet.
- 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
💻 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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