Skip to content

chore: [TS] Transform TouchableHighlight from class to ForwardRef component#44038

Closed
retyui wants to merge 1 commit intofacebook:mainfrom
retyui:chore/retyui/TouchableHighlight-not-a-class
Closed

chore: [TS] Transform TouchableHighlight from class to ForwardRef component#44038
retyui wants to merge 1 commit intofacebook:mainfrom
retyui:chore/retyui/TouchableHighlight-not-a-class

Conversation

@retyui
Copy link
Copy Markdown
Contributor

@retyui retyui commented Apr 11, 2024

Summary:

If you check the source of truth packages/react-native/Libraries/Components/Touchable/TouchableHighlight.js I'll find that TouchableHighlight is a result of React.forwardRef(...) :

const Touchable: React.AbstractComponent<
$ReadOnly<$Diff<Props, {|hostRef: React.Ref<typeof View>|}>>,
React.ElementRef<typeof View>,
> = React.forwardRef((props, hostRef) => (
<TouchableHighlight {...props} hostRef={hostRef} />
));
Touchable.displayName = 'TouchableHighlight';
module.exports = Touchable;

So the TS type isn't correct : (

<TouchableHighlight ref={ref => {   }} />
//                     ^^^ ref should be a `View` (but now it's `TouchableHighlight`)

Breaking changes

As TouchableHighlight isn't class anymore it can't be used as value & type

import {TouchableHighlight} from 'react-native';
const ref = useRef<TouchableHighlight>();
//                ^^^ TS2749: TouchableHighlight refers to a value, but is being used as a type here.
//                            Did you mean typeof TouchableHighlight?

Recommend solution: use build-in react type React.ElementRef

-const ref = useRef<TouchableHighlight>();
+const ref = useRef<React.ElementRef<typeof TouchableHighlight>>();

Also, it possible to use View as type:

-const ref = useRef<TouchableHighlight>();
+const ref = useRef<View>();

Changelog:

[GENERAL] [BREAKING] - [Typescript] Transform TouchableHighlight from JS class to ForwardRef component

Test Plan:

See: packages/react-native/types/__typetests__/index.tsx

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Callstack Partner: Callstack Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Apr 11, 2024
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@dmytrorykun has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Apr 11, 2024
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@dmytrorykun merged this pull request in 401f2fb.

@github-actions
Copy link
Copy Markdown

This pull request was successfully merged by @retyui in 401f2fb.

When will my fix make it into a release? | How to file a pick request?

@retyui retyui deleted the chore/retyui/TouchableHighlight-not-a-class branch April 11, 2024 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. p: Callstack Partner: Callstack Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants