New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array prototype extensions fail circularly with ...args: T[]
, but not args: T[]
#58068
Comments
It's not a crash. (I guess the template should explain what this means since so many people seem to choose this option erroneously.) I'm not sure why this is supposed to be a bug; circularity warnings can be annoying but that doesn't rise to the level of a bug in the language. Yes, you can avoid warnings by changing your code. Note that the error message mentions that the problem is partially due to the lack of a type annotation on const newMethods: {
add<T>(this: T[], ...values: T[]): T;
} = {
add(...values) { this.push(...values); return values[0]; }
}; |
I'm honestly confused why the code is written this way ( |
I created an issue as I feel the two examples should compile the same way, whether it's successful or unsuccessful. I can't see any reason why circular typing would invalidate one example but not the other - that is, I don't see why the spread operator would have any connection to circular type definitions. Explicitly declaring the typing of |
I think this way could allow for more maintainability if extending a prototype with many properties; |
If there's some super straightforward fix here we can take it, but I wouldn't consider this a bug. |
🔎 Search Terms
Typescript array prototype extension spread rest variadic parameter circular failure TS2310 version 5.4.3
🕗 Version & Regression Information
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:
Working (only difference is
...values: T[]
is nowvalues: T[]
- without...
):🙁 Actual behavior
Example using
...values: T[]
fails to compile!🙂 Expected behavior
Both examples should compile successfully.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: