icflorescu/trpc-sveltekit

createMutation (onMutate) context is of type 'unknown'

dihmeetree opened this issue · 1 comments

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

Evertt commented

Hey, I'm new here, just looking around, so I haven't tried this package yet. I would not have expected that this package would also offer a svelte-query utility, but it's great to know that it does.

Anyway I have a few questions:

Is the return type of onMutate inferred correctly by TS? What do you see when you hover your mouse cursor on onMutate? What happens if you explicitly type the return type of onMutate, by for example writing onMutate: async (newTodo): string => {, does that change the type of context in the next function?