Parameters<...> should always be spreadable
See original GitHub issueBug Report
š Version & Regression Information
Version: 4.5.4
āÆ Playground Link
š» Code
const doSomething = (arg:any) => !!arg
type ActionCreators = typeof doSomething
const dispatch = (action: ReturnType<ActionCreators>) => {
console.log(action)
}
const withDispatch =<Action extends ActionCreators>(action:Action)=> (...args: Parameters<Action>)=> dispatch(action(...args)) // A spread argument must either have a tuple type or be passed to a rest parameter. (2556)
const withDispatchApply =<Action extends ActionCreators>(action:Action)=> (...args: Parameters<Action>)=> dispatch(action.apply(null, args)) // no error
const withDispatchCall =<Action extends ActionCreators>(action:Action)=> (...args: Parameters<Action>)=> dispatch(action.call(null, ...args)) // no error
š Actual behaviour
When creating a higher order function that is going to operate on a certain set of functions TSC tells us A spread argument must either have a tuple type or be passed to a rest parameter.(2556)
when we try to spread the arguments to it.
š Expected behaviour
As with action.apply(null, args)
and action.call(null, ...args)
I expect that the inferred type of Parameters
to always be spreadable.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:9
- Comments:12 (3 by maintainers)
Top Results From Across the Web
Spreadability and vulnerability of distributed parameter systems
The system is called. -spreadable if the family. is increasing (in some specific sense) and. -resorbable in the case that the family is...
Read more >Kinetic models of quality parameters of spreadable ... - NCBI
The aim of the study was to assess the usefulness of kinetic models based on the Arrhenius equation as predictive tools for the...
Read more >javascript - Using Parameters<F> infer arguments type - Stack ...
The obvious workaround is therefore to change any to any[] , which shouldn't be a problem because function rest parameters are essentially always...
Read more >Kinetic models of quality parameters of spreadable processed ...
mation of quality parameters referring to spreadable pro- ... not always fully consistent. ... necessary to use the mathematical equation which will.
Read more >Kinetic models of quality parameters of spreadable processed ...
not always fully consistent. ... necessary to use the mathematical equation which will ... parameters during the storage of spreadable Gouda cheese.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Somewhat surprisingly,
(...args: string[]) => void
extends (i.e. is assignable to)(arg: any) => any
but taking itsParameters
gives youstring[]
, while the latter gives you[any]
. So the spread gets disallowed because it doesnāt know whether the (deferred)Parameters
will actually be compatible with the call (in practice it will because youāre calling aT
with aParameters<T>
but that requires higher-level analysis than the compiler is currently capable of).AnyFn
bypasses this because itās explicitly declared with a rest parameterāmeaning you can always call it with a spread per the second part of the quoted error message.For what itās worth, Iāve always felt there should be better error messages for generic cases like this. Rather than an absolute refusal (āis not assignable toā, etc.), the compiler should be clearer that itās only preventing the operation because it canāt verify whether itās safe.
The type
Parameters<Action>
is deferred so thereās not a way for TS to tell that this is always going to end up producing a type that happens to make a correct call. A type assertion is appropriate here