question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Parameters<...> should always be spreadable

See original GitHub issue

Bug Report

šŸ•— Version & Regression Information

Version: 4.5.4

āÆ Playground Link

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:open
  • Created 2 years ago
  • Reactions:9
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
fatcerberuscommented, Jan 29, 2022

Somewhat surprisingly, (...args: string[]) => void extends (i.e. is assignable to) (arg: any) => any but taking its Parameters gives you string[], 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 a T with a Parameters<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.

1reaction
RyanCavanaughcommented, Jan 26, 2022

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

  /*[...]*/ action(...args as [any]);
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found