React Toolkit Query updateCachedData, draft type

Viewed 84

Snippet (apiSlice.ts):

  async onCacheEntryAdded(arg, { updateCachedData, cacheDataLoaded, cacheEntryRemoved }) {
    const ws = new WebSocket("ws://localhost:5994/ws");
    try {
      // Wai for initial query to resolve
      await cacheDataLoaded;

      const listener = (event: MessageEvent) => {
        const data = JSON.parse(event.data);
        console.log("LISTNER", data);

        updateCachedData((draft) => {
          draft.push(data);
        });
      };

      ws.addEventListener("message", listener);
    } catch {
      // No-op for now
    }

    await cacheEntryRemoved;

    ws.close();
  },

Typescript is complaining that Property 'push' does not exist on type 'void'.ts(2339). When hovering over draft argument, the inferred type is any. Looking on GitHub public repos, this seems to work for everyone else..

Why is TypeScript complaining about this, is the type not inferred via RTK Query?

As a result, I get this error when trying to execute draft.push:

Uncaught TypeError: draft.push is not a function
    at apiSlice.ts:187:21
    at e.produce (immer.esm.mjs:1:16170)
    at e.produceWithPatches (immer.esm.mjs:1:16741)
    at rtk-query.esm.js:489:44
    at index.js:16:1
    at redux-toolkit.esm.js:317:1
    at Object.dispatch (redux.js:691:1)
    at __spreadProps.updateCachedData (rtk-query.esm.js:1451:112)
    at WebSocket.listener (apiSlice.ts:186:13)
0 Answers
Related