-
-
Notifications
You must be signed in to change notification settings - Fork 606
fix: fix library typing for React 19 #3509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kkafar
wants to merge
10
commits into
main
Choose a base branch
from
@kkafar/remove-react-types-resolution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
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
It seems that in React 19.0 if you pass `null` as the initArg to the `React.useRef` then nullable type will be inferred anyway. So the `searchBarRef` is of type `React.RefObject<SearchBarCommands | null>` and we pass it to ref whose type is determined by `NativeSearchBar` type definition, which assumes `React.RefObject<SearchBarCommands>`, hence the error. I believe applying this patch shouldn't be treated as breaking change, but rather as a type fix. For anyone typechecking / using this ref it must have been annoying. The error would have to be either suppressed or ignored.
definition
`ElementRef` got deprecated in `React@19` and moreover using it results
in following error:
```
Diagnostics:
1. Type 'ComponentType<NativeProps & { ref?: RefObject<SearchBarCommands | null> | undefined; }> & SearchBarCommandsType' does not satisfy the constraint 'ForwardRefExoticComponent<any> | (new (props: any, context: any) => Component<any, {}, any>) | ((props: any) => ReactNode) | keyof IntrinsicElements'.
Type 'FunctionComponent<NativeProps & { ref?: RefObject<SearchBarCommands | null> | undefined; }> & SearchBarCommandsType' is not assignable to type 'ForwardRefExoticComponent<any> | (new (props: any, context: any) => Component<any, {}, any>) | ((props: any) => ReactNode) | keyof IntrinsicElements'.
Property '$$typeof' is missing in type 'FunctionComponent<NativeProps & { ref?: RefObject<SearchBarCommands | null> | undefined; }> & SearchBarCommandsType' but required in type 'ForwardRefExoticComponent<any>'. [2344]
2. 'ElementRef' is deprecated. [6385]
```
I do not understand it fully. I don't even want to ;D Swapping for
`ComponentRef` does the trick.
My only worry is that this potentially could be considered a breaking
change. HOWEVER, if this fixes an issue? I wanna be liberal here and
treat is only as a fix & not a breaking change.
This is not breaking as it allows for wider gamut of types to be accepted. Ideally we should allow for `React.Ref` there, as suggested here: #2724 (comment), but it'd require some more internal changes. Co-authored-by: Simone Gauli <Pnlvfx@users.noreply.github.com>
61bdfcf to
7d322aa
Compare
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.
Description
Closes https://github.com/software-mansion/react-native-screens-labs/issues/756
This resolution leads to type problems when using new react 19.2 features, such as rendering context directly as
<Context>instead of<Context.Provider>.I tracked that is has been added in 570923d - there is no
explanation in the commit message, why it has been added. I can now only assume that it was to enforce single react types across library & example code.
We now support React Native since 0.81 anyway & it comes with React 19 => all these changes can be treated as fixes and not breaking changes.
See detailed changelog below.
Changes
Remove resolution for "@types/react"
Fix type problems with
usePreviousFix type errors in
Screen.web.tsxFix type errors in
ScreenStackHeaderConfig.tsxFix type errors in
ScreenStackHeaderConfig.web.tsxFix typing for SearchBar ref
It seems that in React 19.0 if you pass
nullas the initArgto the
React.useRefthen nullable type will be inferred anyway.So the
searchBarRefis of typeReact.RefObject<SearchBarCommands | null>and we pass it to ref whose type is determined byNativeSearchBartype definition, which assumesReact.RefObject<SearchBarCommands>, hence the error.I believe applying this patch shouldn't be treated as breaking change,
but rather as a type fix. For anyone typechecking / using this ref
it must have been annoying. The error would have to be either
suppressed or ignored.
Change
ElementReftoComponentRefinNativeSearchBarReftype definitionElementRefgot deprecated inReact@19and moreover using it resultsin following error:
I do not understand it fully. I don't even want to ;D Swapping for
ComponentRefdoes the trick.My only worry is that this potentially could be considered a breaking
change. HOWEVER, if this fixes an issue? I wanna be liberal here and
treat is only as a fix & not a breaking change.
Formatter
Allow for nullable ref param in SearchBarCommands types