Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
5 replies
noctate

How to type object like this as props

Heyy I play a little with trpc and optimistic updates and I wonder how can I type object like this when I pass it as props.
const addTodo = api.todos.createTodo.useMutation({
    async onMutate(newTodo) {
      // Cancel outgoing fetches (so they don't overwrite our optimistic update)
      await utils.todos.getAllTodos.cancel();

      // Get the data from the queryCache
      const prevData = utils.todos.getAllTodos.getData();

      // Optimistically update the data with our new post
      utils.todos.getAllTodos.setData(
        undefined,
        (old) => [...(old as Todo[]), newTodo] as Todo[]
      );

      // Return the previous data so we can revert if something goes wrong
      return { prevData };
    },
    onError(err, newPost, ctx) {
      // If the mutation fails, use the context-value from onMutate
      utils.todos.getAllTodos.setData(undefined, ctx?.prevData);
    },
    onSettled() {
      // Sync with server once mutation has settled
      void utils.todos.getAllTodos.invalidate();
    },
  });

then
const CreateTodo = ({ addTodo }: ???) => {}
Was this page helpful?