property does not exist on type 'unknown' using listenerApi.getState() in createListenerMiddleware

Viewed 33

In typescript I'm trying to access current state via getState() provided in the listenerApi.

from the docs we know that state is 'unknown' because at the time of middleware being loaded the state is yet to be initialize.

I'm unclear how exporting the placeholder types from the middleware, as from docs, helps with the call to getState() within the effect, indeed I am calling getState() from within the listener.

When I add the type exports the error persisits.

here is the code that fails:

listenerMiddleware.startListening({
  actionCreator: taskRetrieve,
  effect: async (action, listenerApi) => {
    // takeLeading: Starts listener on first action, ignores others until task completes
    listenerApi.unsubscribe();

    const currentTask =  listenerApi.getState().todos.tasks[0];
    
    // will do something async here later...

    listenerApi.subscribe();
  },
});

and the ts error:

Property 'todos' does not exist on type 'unknown'.ts(2339)

I'm potentially missing something obvious here...

0 Answers
Related