Skip to content

fix: preserve non-nullable ref return types - #2956

Merged
crazylxr merged 2 commits into
alibaba:masterfrom
dvd233:fix/non-nullable-ref-types
Sep 1, 2026
Merged

fix: preserve non-nullable ref return types#2956
crazylxr merged 2 commits into
alibaba:masterfrom
dvd233:fix/non-nullable-ref-types

Conversation

@dvd233

@dvd233 dvd233 commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

🤔 This is a ...

  • New feature
  • Bug fix
  • Site / documentation update
  • Demo update
  • TypeScript definition update
  • Bundle size optimization
  • Performance optimization
  • Enhancement feature
  • Internationalization
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Other (about what?)

🔗 Related issue link

Fixes #2808

💡 Background and solution

After the React 19 type upgrade, the inferred declarations for useLatest and useUnmountedRef changed from their documented mutable, non-nullable ref contracts to RefObject. Because ahooks still supports React 16–19 and older React type packages define RefObject<T>.current as T | null, consumers can see a nullable current even though both hooks initialize it synchronously and never introduce null themselves.

This change explicitly preserves the public contracts as MutableRefObject<T> and MutableRefObject<boolean>, whose current stays non-nullable across the supported React type versions. It also adds declaration-emission regression tests alongside the existing runtime coverage. No runtime behavior changes.

Validation:

  • vitest run packages/hooks/src/__tests__/refTypes.spec.ts packages/hooks/src/useLatest/__tests__/index.spec.ts packages/hooks/src/useUnmountedRef/__tests__/index.spec.ts --no-file-parallelism --maxWorkers=1 (3 files, 5 tests passed)
  • vitest run --color (93 files, 406 tests passed)
  • tsc --noEmit -p packages/hooks/tsconfig.json
  • declaration-only emit verified both public signatures

📝 Changelog

Language Changelog
🇺🇸 English Restore non-nullable mutable ref return types for useLatest and useUnmountedRef.
🇨🇳 Chinese 恢复 useLatestuseUnmountedRef 非空可变 ref 的返回类型。

☑️ Self Check before Merge

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ dvd233
❌ crazylxr
You have signed the CLA already but the status is still pending? Let us recheck it.

@crazylxr crazylxr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@pkg-pr-new

pkg-pr-new Bot commented Sep 1, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/ahooks@2956
npm i https://pkg.pr.new/@ahooks.js/use-url-state@2956

commit: 560bcb9

@crazylxr
crazylxr merged commit cf96640 into alibaba:master Sep 1, 2026
8 of 9 checks passed
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.

useLatest useUnmountedRef 类型带有 null

3 participants