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

[7.4.0] Incorrect return types with TanStack Query and fetch #1801

Closed
rminami opened this issue Jan 9, 2025 · 1 comment · Fixed by #1805
Closed

[7.4.0] Incorrect return types with TanStack Query and fetch #1801

rminami opened this issue Jan 9, 2025 · 1 comment · Fixed by #1805
Assignees
Labels
tanstack-query TanStack Query related issue
Milestone

Comments

@rminami
Copy link

rminami commented Jan 9, 2025

What are the steps to reproduce this issue?

When generating an orval client with v7.4.0 using TanStack Query and fetch, orval emits an invalid return type for the response data.

const {
  data
} = useGetUsers(
  { limit, offset },
  { request: { accessKey }, query: { placeholderData: keepPreviousData } },
);

In v7.3.0, the return type was User[] | undefined, but with v7.4.0 it emits the following type error.

Type '<T>(previousData: T | undefined) => T | undefined' is not assignable to type 'unknown[]'.

Configuration details

The orval config is

import { defineConfig } from "orval";

export default defineConfig({
  exchange: {
    input: {
      target: "openapi.yml",
    },
    output: {
      client: "react-query",
      mode: "tags-split",
      baseUrl: process.env.BASE_URL,
      workspace: "src/lib/api",
      target: "index.ts",
      schemas: "models",
      prettier: true,
      clean: true,
      override: {
        query: {
          useSuspenseQuery: true,
        },
        mutator: {
          path: "../custom-client.ts",
          name: "customRequest",
        },
        useTypeOverInterfaces: true,
      },
    },
    hooks: {
      afterAllFilesWrite: "prettier -w src/lib/api/**/*.ts",
    },
  },
});

and the custom client is

export const customRequest = async <T>(
  config: {
    url: string;
    method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
    params?: Record<string, unknown>;
    headers?: Record<string, unknown>;
    data?: unknown;
    responseType?: string;
    signal?: AbortSignal;
  },
  options?: { accessKey: string | undefined },
): Promise<T> => {
  const params = parseParams(config.params);
  const res = await fetch(config.url + params, {
    ...config,
    ...options,
    body: JSON.stringify(config.data),
    headers: {
      ...config.headers,
      ...(options?.accessKey != null
        ? {
            Authorization: `Bearer ${options.accessKey}`,
          }
        : []),
    },
  });
  return await res.json();
};

What versions are you using?

System:
  OS: macOS 15.2
  CPU: (12) arm64 Apple M3 Pro
  Memory: 3.34 GB / 36.00 GB
  Shell: 5.9 - /bin/zsh
npmPackages:
  @tanstack/react-query: ^5.63.0 => 5.63.0
  orval: ^7.4.0 => 7.4.0
  react: ^19.0.0 => 19.0.0
  zod: ^3.24.1 => 3.24.1
@melloware melloware added this to the 7.4.1 milestone Jan 9, 2025
@melloware melloware added bug Something isn't working tanstack-query TanStack Query related issue and removed bug Something isn't working labels Jan 9, 2025
@rminami
Copy link
Author

rminami commented Jan 9, 2025

Thank you for the quick fix! Confirmed this is no longer an issue in 7.4.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tanstack-query TanStack Query related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants