trpc-sveltekit icon indicating copy to clipboard operation
trpc-sveltekit copied to clipboard

createMutation (onMutate) context is of type 'unknown'

Open dihmeetree opened this issue 1 year ago • 1 comments
trafficstars

Doing the following on my Svelte page (Optimistic Updating):

const api = trpc($page);
const utils = api.createUtils();

const todos = utils.authed.todos;

const addTodo = api.authed.todos.add.createMutation({
  onMutate: async (newTodo) => {
    await todos.list.cancel();
    const previousTodos = todos.list.getData();
    todos.list.setData(undefined, (old) => [
      ...(old ?? []),
      {
        id: '0',
        data: newTodo.data
      }
    ]);
    return { previousTodos };
  },
  onError: (err, newTodo, context) => {
    console.log(err);
    console.log(context);
    todos.list.setData(undefined, context.previousTodos);
  },
  onSuccess: () => {
    newTodo = '';
    todos.list.refetch();
  }
});

However i'm getting an error:

'context' is of type 'unknown'.ts(18046)

image

Not sure if it's a bug, or if i'm doing something wrong. Followed the guide here https://tanstack.com/query/v4/docs/framework/react/guides/optimistic-updates

dihmeetree avatar Feb 05 '24 14:02 dihmeetree