Skip to content

Enhanced Type Safety for Query and Path Parameters in swr-openapi #2410

@yashpandit

Description

@yashpandit

swr-openapi version

5.3.1

Description

The current implementation of createQueryHook and createInfiniteHook doesn't enforce exact object literal validation for query and path parameters, allowing invalid properties to pass TypeScript compilation without errors.

This leads to runtime issues and reduces the type safety benefits that developers expect from a TypeScript-first OpenAPI integration.

Reproduction

import createClient from 'openapi-fetch';
import { createQueryHook } from 'swr-openapi';
import type { paths } from './generated'; // From openapi-typescript

const client = createClient<paths>();
const useQuery = createQueryHook(client, 'api');

// ❌ This should cause a TypeScript error but doesn't
const { data } = useQuery('/pets', {
  params: {
    query: {
      limit: 10,
      invalid_property: 'should error', // No TypeScript error!
    },
  },
});

// ❌ Same issue with infinite queries
const useInfiniteQuery = createInfiniteHook(client, 'api');
const { data: infiniteData } = useInfiniteQuery('/pets', pageIndex => ({
  params: {
    query: {
      limit: 10,
      another_invalid_prop: 'also no error', // No TypeScript error!
    },
  },
}));

Expected result

Expected behavior: TypeScript should show compilation errors for invalid_property and another_invalid_prop.

Actual behavior: Code compiles without errors, potentially causing runtime issues.

Extra

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingswr-openapiRelevant to swr-openapi library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions