Skip to content

Conversation

CO0Ki3
Copy link

@CO0Ki3 CO0Ki3 commented Sep 25, 2025

Fixes #5086

fix(types): ensure non-undefined data on isSuccess with exactOptionalPropertyTypes

When TypeScript exactOptionalPropertyTypes: true is enabled, UseQueryStateDefaultResult
could infer data as undefined even on isSuccess branches. This patch explicitly sets
data: ResultTypeFrom<D> (and currentData: ResultTypeFrom<D> when !isFetching)
to provide correct type narrowing while keeping isLoading as data: undefined.

  • Replace Required<Pick<..., 'data'>> with explicit data: ResultTypeFrom<D>
  • Keep data: undefined only on isLoading branch
  • Make currentData required on isSuccess && !isFetching
  • Preserve fulfilledTimeStamp/error constraints
image

Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit e7ea549:

Sandbox Source
@examples-query-react/basic Configuration
@examples-query-react/advanced Configuration
@examples-action-listener/counter Configuration
rtk-esm-cra Configuration

Copy link

netlify bot commented Sep 25, 2025

Deploy Preview for redux-starter-kit-docs ready!

Name Link
🔨 Latest commit e7ea549
🔍 Latest deploy log https://app.netlify.com/projects/redux-starter-kit-docs/deploys/68d4d90fc0876400089c3c98
😎 Deploy Preview https://deploy-preview-5088--redux-starter-kit-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link

codesandbox bot commented Sep 25, 2025

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

@CO0Ki3
Copy link
Author

CO0Ki3 commented Sep 26, 2025

Suggestion: split each union branch into a dedicated named type. It reads cleaner and improves maintainability.

as-is

type UseQueryStateDefaultResult<D extends QueryDefinition<any, any, any, any>> =
  TSHelpersId<
    | TSHelpersOverride<
        Extract<
          UseQueryStateBaseResult<D>,
          { status: QueryStatus.uninitialized }
        >,
        { isUninitialized: true }
      >
    | ...

suggest to-be

type QueryUninitialized<D extends QueryDefinition<any, any, any, any>> =
  TSHelpersOverride<
    Extract<UseQueryStateBaseResult<D>, { status: QueryStatus.uninitialized }>,
    { isUninitialized: true }
  >

...

type UseQueryStateDefaultResult<D extends QueryDefinition<any, any, any, any>> = 
  TSHelpersId<
    | QueryUninitialized<D>
    | ...

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.

RTK Query data and error not narrowed when exactOptionalPropertyTypes is enabled
1 participant