Skip to content

typescript error querykey is missing #7732

@psm-solution-group

Description

@psm-solution-group

Describe the bug

Hello,

I use Optismistic Update and I return prevData and newData on the onMutate function.

I get this type error on prevData

Type '[QueryKey, IDashboardData | undefined][]' is missing the following properties from type 'IDashboardData': ill, cancellations, holidays, meetings, and 4 more.ts(2740)

And this type error on newData

Type '(TTaggedQueryKey extends { [dataTagSymbol]: infer TaggedValue; } ? TaggedValue : TQueryFnData) | undefined' is not assignable to type 'IDashboardData'.
  Type 'undefined' is not assignable to type 'IDashboardData'.ts(2322)

here are my interface/types

export interface IDashboardData {
  ill: DashboardStatisticPercentage;
  cancellations: DashboardStatisticPercentage;
  holidays: DashboardStatisticPercentage;
  meetings: DashboardMeetingsTable[];
  userRequests: DashboardUserRequest[];
  events: DashboardCurrentEvents[];
  workerTimes: DashboardLastWorkerTimes[];
  availableCars: DashboardAvailableCars[];
}

Your minimal, reproducible example

/

Steps to reproduce

here is my code:

  const { mutateAsync } = useMutation({
    mutationFn: async (params: IConfirmAccountMutation) => {
      const res = await axiosPrivate.post(`${URL}/auth/confirmAccount`, params, {
        headers: headersJSON,
        withCredentials: true
      });
    
      const data: IReturnApi<null> = await res.data;
      return data;
    },
    onMutate: async (params) => {
      console.log(params);
      await queryClient.cancelQueries({queryKey: ["dashboard"]});

      const prevData: IDashboardData = queryClient.getQueriesData<IDashboardData>({queryKey: ["dashboard"]});

      const newData: IDashboardData = queryClient.setQueryData<IDashboardData | undefined>(["dashboard"], (old: IDashboardData) => {
        return {
          ...old,
          userRequests: old.userRequests.filter((e => e.id !== params.id))
        }
      });

      return {
        prevData,
        newData
      };
    },
    onSuccess: (data) => {
      toast.success(data.message);
    },
    onError: (err: AxiosError<IReturnApi<IValidationError[]>>, variables: any, context: IContextQuery | undefined) => {
      console.log(context);
      console.log(context);
      if(err.response) {
        toast.error(err.response.data.message);
      } else {
        toast.error("Es ist ein Fehler aufgetreten. Code: #1_USER_REQUEST_CONFIRM");
      }
    }
  })

Expected behavior

/

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

windows 10
vscode

Tanstack Query adapter

react-query

TanStack Query version

latest

TypeScript version

latest

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions