You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
arguments errors with Argument of type 'IArguments' is not assignable to parameter of type '[]'.(2345)
π Expected behavior
No error.
Additional information about the issue
f.apply(null, arguments) is a universal idiom for decades, albeit one that's fallen out of favor in lieu of f(...arguments) - and when one needs to set the receiver, f.apply(O, arguments) is the only choice.
Somewhat related to #68307; basically, there should probably be an "arraylike" type that all of arrays and arguments and strings and nodelists can assign to.
I canβt use a type assertion in jsdoc, so thatβs not an option for me, nor do i wish to declare a global type (esp one thatβs βunsafeβ per that issue comment), so it doesnβt seem like i have any workaround.
Thanks, altho i wouldnβt want to cast it to any - and in my particular case i have a handful of places and Iβd prefer not to litter my code with casts.
I was going to suggest this could be fixed by automagically typing arguments as IArguments & Parameters<typeof CALLEE> but for some reason intersected tuples can not be spread.
functionabc(a: 'a',b: 'b',c:'c'){varx=argumentsasParameters<typeofabc>&IArguments;// WORKS: Accepts (tuple & IArguments) because it is // assignable to an arrayabc.apply(null,x);// ERROR: Can spread tuple but not (tuple & IArguments)abc(...x);// A spread argument must either have a tuple // type or be passed to a rest parameter. (2556)}
You can also turn off strictBindCallApply π« . The "right" fix would be for b/c/a to take ArrayLike<T> versions of their argument tuples, but we don't have any type like that right now and it would be a very expensive fix that would really only pay off in this exact scenario.
It definitely would be amazing to have the proper ArrayLike<T> fix - for a lot of the builtin APIs - but this can sit open pending that, and I'll use the workaround of turning off strictBindCallApply.
Daniel Rosenwasser (@DanielRosenwasser) ah right thanks, i'd figured that out since then (i had to disable the no-extra-parens eslint rule). i can't use a non-null assertion, still, tho :-/
altho casting it to "any" doesn't feel like an option to me :-) what i'd want is to cast it to unknown[] or Parameters<f> or something
Activity
MartinJohns commentedon Jan 25, 2024
Duplicate of #36470. The issue provides a workaround using a local declaration that supports
IArgumentsforapply. Alternatively use a type assertion.ljharb commentedon Jan 25, 2024
I canβt use a type assertion in jsdoc, so thatβs not an option for me, nor do i wish to declare a global type (esp one thatβs βunsafeβ per that issue comment), so it doesnβt seem like i have any workaround.
Andarist commentedon Jan 25, 2024
You can always assert it at the call site:
ljharb commentedon Jan 25, 2024
Thanks, altho i wouldnβt want to cast it to
any- and in my particular case i have a handful of places and Iβd prefer not to litter my code with casts.MartinJohns commentedon Jan 25, 2024
π€·ββοΈ You only have those two options.
ljharb commentedon Jan 25, 2024
Then i guess itβs good i filed this issue :-)
snarbies commentedon Jan 29, 2024
I was going to suggest this could be fixed by automagically typing
argumentsasIArguments & Parameters<typeof CALLEE>but for some reason intersected tuples can not be spread.RyanCavanaugh commentedon Jan 29, 2024
You can also turn off
strictBindCallApplyπ« . The "right" fix would be for b/c/a to takeArrayLike<T>versions of their argument tuples, but we don't have any type like that right now and it would be a very expensive fix that would really only pay off in this exact scenario.ljharb commentedon Jan 29, 2024
hmm, i wasn't aware of that setting, but i usually am loath to disable any of the strict settings.
Would it be possible to augment the type of these methods to take
| IArgumentsas a "cheap" fix, without needing to disable that option?Unfortunately this exact scenario is absurdly common in any code that's not transpiled and needs to support pre-ES6 envs :-/
RyanCavanaugh commentedon Jan 29, 2024
Adding
| IArgumentsis basically the same as turning offstrictBindCallApply, since every array is anIArgumentsljharb commentedon Jan 29, 2024
ah ok, gotcha.
It definitely would be amazing to have the proper
ArrayLike<T>fix - for a lot of the builtin APIs - but this can sit open pending that, and I'll use the workaround of turning off strictBindCallApply.DanielRosenwasser commentedon Jan 29, 2024
Just as a heads up, you can. They are parenthesized expressions with an
/** @type {...} */comment preceding them.Playground Link
ljharb commentedon Jan 29, 2024
Daniel Rosenwasser (@DanielRosenwasser) ah right thanks, i'd figured that out since then (i had to disable the no-extra-parens eslint rule). i can't use a non-null assertion, still, tho :-/
altho casting it to "any" doesn't feel like an option to me :-) what i'd want is to cast it to
unknown[]orParameters<f>or somethingFix dwarf extension build