Skip to content

Ref Typings #2885

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

Merged
merged 23 commits into from
Jan 14, 2024
Merged

Ref Typings #2885

merged 23 commits into from
Jan 14, 2024

Conversation

nitzanyiz
Copy link
Collaborator

Description

Added RefInterface to the our forwardRef function. Adding this generic sets the type of the ref to the specific interface so when passing ref to a one of our components it will show a ts error when passing an incompatible interface ref.

Changelog

Added generic typings to the forwardRef and in the asBaseComponent usage.

Additional info

None

@nitzanyiz nitzanyiz requested a review from ethanshar January 11, 2024 14:12
@nitzanyiz nitzanyiz marked this pull request as ready for review January 11, 2024 14:15
Copy link
Collaborator

@ethanshar ethanshar left a comment

Choose a reason for hiding this comment

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

Looks great!
Wrote few small comments..

const customSlider = useRef<typeof Incubator.Slider>();
const negativeSlider = useRef<typeof Incubator.Slider>();
const rangeSlider = useRef<typeof Incubator.Slider>();
const slider = useRef<Incubator.SliderRef>(null);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need to pass null here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

When using it without null, the type of the ref is Ref<SliderRef | undefined> which causes an error. This error also occurs with regular react native refs. For example using React Native TextInput.
image

@@ -213,4 +213,5 @@ GradientSlider.displayName = 'GradientSlider';
GradientSlider.types = GradientSliderTypes;

// eslint-disable-next-line max-len
export default asBaseComponent<GradientSliderProps, typeof GradientSlider>(forwardRef(asSliderGroupChild(forwardRef(GradientSlider))));
// @ts-expect-error
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you know why we have an error here?
If it's too complicated we can solve it another time..

@ethanshar ethanshar merged commit 3e35a86 into master Jan 14, 2024
@nitzanyiz nitzanyiz deleted the infra/RefTypings branch January 14, 2024 12:18
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.

2 participants