fix: preserve non-nullable ref return types - #2956
Merged
Merged
Conversation
|
|
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤔 This is a ...
🔗 Related issue link
Fixes #2808
💡 Background and solution
After the React 19 type upgrade, the inferred declarations for
useLatestanduseUnmountedRefchanged from their documented mutable, non-nullable ref contracts toRefObject. Because ahooks still supports React 16–19 and older React type packages defineRefObject<T>.currentasT | null, consumers can see a nullablecurrenteven though both hooks initialize it synchronously and never introducenullthemselves.This change explicitly preserves the public contracts as
MutableRefObject<T>andMutableRefObject<boolean>, whosecurrentstays 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📝 Changelog
useLatestanduseUnmountedRef.useLatest和useUnmountedRef非空可变 ref 的返回类型。☑️ Self Check before Merge