Skip to content

Conversation

yashpandit
Copy link
Contributor

@yashpandit yashpandit commented Aug 14, 2025

Changes

Closes #2410

Summary

This PR tightens TypeScript excess-property checks for request init objects in swr-openapi:

  • Enforces excess-property checking for useQuery init objects by using the concrete inferred type in the function signature.
  • Adds regression tests to prevent future drift.
  • Updates docs to show DX-friendly patterns for exact param shapes with useInfinite.

No runtime behavior changes.

Motivation

Previously, TypeScript accepted extra keys in params.query/params.path without error which could hide mistakes

What changed

  • packages/swr-openapi/src/query-base.ts

    • Changed useQuery signature to use R["Init"] directly in the parameter tuple:
      • Excess properties in object literals now trigger errors.
      • Optional/required init behavior still determined via RequiredKeysOf<R["Init"]>.
  • packages/swr-openapi/src/__test__/types.test-d.ts

    • Added “excess property checks” tests:
      • useQuery: rejects extra properties in params.query, params.path, params.header.
  • docs/swr-openapi/use-infinite.md

    • Added “Enforcing exact param shapes” section showing two minimal patterns:
      • Typed variable with TypesForRequest<paths, "get", "...">["Init"]
      • Inline satisfies with the same type

Before/After

  • useQuery (before): extra keys could pass type-check in some cases.
  • useQuery (after): passing an object literal with extra keys now errors reliably.
useQuery("/pet/findByStatus", {
  params: {
    query: {
      status: "available",
      // now correctly rejected
      invalid_property: "nope",
    },
  },
});
  • useInfinite loader (recommended patterns to force exact shape):
// Option 1: typed variable
type FindByStatus = TypesForRequest<paths, "get", "/pet/findByStatus">;
const init: FindByStatus["Init"] = {
  params: { query: { status: "available" } },
  // extra properties here are rejected by TS
};
useInfinite("/pet/findByStatus", () => init);

// Option 2: satisfies
useInfinite(
  "/pet/findByStatus",
  () =>
    ({
      params: { query: { status: "available" } },
      // extra properties here are rejected by TS
    }) satisfies TypesForRequest<paths, "get", "/pet/findByStatus">["Init"]
);

Tests

  • Added type-only tests to assert excess property checks for both useQuery and useInfinite (query + path).
  • All tests pass.

Docs

  • Documented strict param patterns for infinite loaders in docs/swr-openapi/use-infinite.md.

Checklist

  • Unit tests updated
  • docs/ updated (if necessary)

@yashpandit yashpandit requested a review from a team as a code owner August 14, 2025 13:43
@yashpandit yashpandit requested a review from htunnicliff August 14, 2025 13:43
Copy link

changeset-bot bot commented Aug 14, 2025

🦋 Changeset detected

Latest commit: 36bf3a5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
swr-openapi Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

netlify bot commented Aug 14, 2025

Deploy Preview for openapi-ts failed.

Name Link
🔨 Latest commit 36bf3a5
🔍 Latest deploy log https://app.netlify.com/projects/openapi-ts/deploys/689de938659c43000878a146

@yashpandit
Copy link
Contributor Author

@htunnicliff can you plz take a look? would like to get this change in if possible. thanks!

@htunnicliff
Copy link
Member

Thank you for finding this issue @yashpandit!

Since this would be useful for other parts of the init types, I expanded your changes to include more areas of init in other swr-openapi functions: #2420

I was able to use Exact<T> to facilitate this, so I don't think a docs update is needed. Please take a look and let me know what you think!

@yashpandit yashpandit closed this Aug 18, 2025
@yashpandit
Copy link
Contributor Author

closing in favor of #2420

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants