2

It is probably a noobish question, but how do I deal with the Typescript error TS2344?

Type 'State' does not satisfy the constraint '(state: any, ...args: any[]) => any'.

Here is the code fragement of my sagas.ts where the error occurs

function* loadPageFull(action: Actions.LoadPageFullAction ) {
    if (!action.id)
        return;
    const currentPageFull: Interfaces.PageFull = 
      yield (select<Top.State>(Selectors.getPageFull(action.id))); // <-- error occurs here

    if (!currentPageFull || action.forceReload) {
        // here we query the API of the backend and return some JSON
    }
}

The problem seems to be the Top.State in the line with the yield. The curious thing is that I did not have the error before updating Typescript to version 3.6.4.

EDIT: The getPageFull is defined in selectors.ts as

const getPageFullInner = (state: State, id: number) => state.pagesFull.get(id);
export const getPageFull = (id: number) => (state: Top.State) 
    => getPageFullInner(foobar(state), id);

The foobar() function is also defined there

export const foobar = (state: State) => state.foobar;

References

B--rian
  • 5,578
  • 10
  • 38
  • 89

1 Answers1

1

The signature of select is:

export function select<Fn extends (state: any, ...args: any[]) => any>(
  selector: Fn,
  ...args: Tail<Parameters<Fn>>
): SelectEffect

So the first generic parameter has to be a function type (specifically (state: any, ...args: any[]) => any), but you're giving it State.

You don't need to specify the generic parameter, as it can be inferred from the argument, so you can simply write:

select(Selectors.getPageFull(action.id))
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • I just now found https://stackoverflow.com/questions/48632123/what-do-the-angle-brackets-and-asterisk-mean-in-react which helped me to understand. How is this prototyping with angle brackets called? Do you have a link to an official documentation on that? – B--rian Oct 29 '19 at 10:35
  • The angle brackets are used for generics: https://www.typescriptlang.org/docs/handbook/generics.html – Emil Laine Oct 29 '19 at 10:43