Skip to content

Conversation

@harikapadia999
Copy link

Description

Fixes #7088

The useCustom hook's result.data type was incorrectly typed as always defined, causing TypeScript errors when using optional chaining or checking for undefined values.

Changes

Type System

  • Updated UseCustomReturnType to explicitly mark result.data as TData | undefined
  • Added comprehensive JSDoc documentation explaining when data can be undefined
  • Removed EMPTY_OBJECT fallback that masked the undefined state

Runtime Behavior

  • Now correctly returns undefined for result.data during:
    • Loading state (query is pending)
    • Error state (query failed)
    • Disabled queries (enabled: false)
    • Before first execution

Testing

  • Added 8 comprehensive test cases covering:
    • Undefined data during loading
    • Undefined data when query is disabled
    • Undefined data on error
    • Empty array vs undefined distinction
    • Null data handling
    • Optional chaining safety
    • State transition from undefined to defined

Impact

Enables proper null-safety checks - Users can now safely use optional chaining
Aligns types with runtime - TypeScript types now match actual behavior
Backward compatible - Existing code continues to work
Better DX - No more type errors for correct code

Example

Before (Type Error):

const { result } = useCustom({ url: "/api/data", method: "get" });
const items = result.data?.items; // ❌ Type error: data is never undefined

After (Works Correctly):

const { result } = useCustom({ url: "/api/data", method: "get" });
const items = result.data?.items; // ✅ Works! data can be undefined

The useCustom hook's result.data type was incorrectly typed as always defined,
causing TypeScript errors when trying to use optional chaining or check for undefined.

Changes:
- Updated UseCustomReturnType to explicitly allow undefined for result.data
- Added comprehensive JSDoc explaining when data can be undefined
- Removed EMPTY_OBJECT fallback that masked the undefined state
- Now correctly returns undefined during loading, errors, or disabled queries

This fix aligns the TypeScript types with the actual runtime behavior,
preventing type errors and enabling proper null-safety checks.

Fixes refinedev#7088
…ed handling

Add extensive test coverage for the bug fix in refinedev#7088:
- Test that result.data is undefined during loading state
- Test that result.data is undefined when query hasn't executed
- Test that result.data is undefined on error
- Test TypeScript type safety with undefined checks
- Test edge cases with empty responses and null data
- Test transition from undefined to defined state

These tests ensure the type fix correctly reflects runtime behavior
and prevents regressions.
@harikapadia999 harikapadia999 requested a review from a team as a code owner December 16, 2025 06:20
@changeset-bot
Copy link

changeset-bot bot commented Dec 16, 2025

🦋 Changeset detected

Latest commit: d65825e

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

This PR includes changesets to release 1 package
Name Type
@refinedev/core 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

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.

[BUG] useCustom return type is incorrect and undocumented v5 breaking change

1 participant