Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createMutation (onMutate) context is of type 'unknown' #111

Open
dihmeetree opened this issue Feb 5, 2024 · 1 comment
Open

createMutation (onMutate) context is of type 'unknown' #111

dihmeetree opened this issue Feb 5, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@dihmeetree
Copy link

dihmeetree commented Feb 5, 2024

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 dihmeetree added the bug Something isn't working label Feb 5, 2024
@dihmeetree dihmeetree changed the title createMutation context is of type 'unknown' createMutation (onMutate) context is of type 'unknown' Feb 5, 2024
@Evertt
Copy link

Evertt commented Feb 11, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants