Skip to content

Migrate Hint component to TypeScript #1281

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 11 commits into from
May 5, 2021
Merged

Conversation

saulve
Copy link
Contributor

@saulve saulve commented Apr 29, 2021

Description

This PR migrates Hint component and its example screen to TypeScript.

Changelog

Removes manual typings in Hint.d.ts

visible={this.showHint}
animationType="none"
transparent
onBackgroundPress={onBackgroundPress}
onRequestClose={onBackgroundPress}
onRequestClose={onBackgroundPress as () => void}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why the change of type to () => void?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because onRequestClose expects a function with () => void signature, however onBackgroundPress has (event: GestureResponderEvent) => void. Therefore I'm adding a type assertion here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks like a bug, because the user that sends the onBackgroundPress can expect to receive an event and may rely on it. I think you need to change the prop to:

onBackgroundPress?: (event?: GestureResponderEvent) => void;

(and remove the as () => void)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's actually the other way around.

ModalProps ( in src/components/modal/index.tsx) is defined:

/**
  * allow dismissing a modal when clicking on its background
  */
onBackgroundPress?: (event: GestureResponderEvent) => void;

Whereas onRequestClose is defined:

/**
  * The `onRequestClose` prop allows passing a function that will be called once the modal has been dismissed.
  * _On the Android platform, this is a required function._
  */
onRequestClose?: () => void;

IMO it's important to keep the onBackgroundPress definition as is because if the user uses onBackgroundPress prop then event should always be defined (as you've described). onRequestClose, however does not expect a function argument, so we can override it. I think the only reason we use onRequestClose is because it's a required function for android (onBackgroundPress is what should actually dismiss the modal) . Alternatively, maybe we could even have something like onRequestClose={this.noop} (noop = () => {})?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we should take this offline.

@M-i-k-e-l M-i-k-e-l self-requested a review May 3, 2021 08:34
Copy link
Collaborator

@M-i-k-e-l M-i-k-e-l left a comment

Choose a reason for hiding this comment

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

Nice work and thank you!

I've left some comments, you also need to update generatedTypes (it's a little duplication of src/index :/ ).
There are some things that I'd like to see changed, but I'm not sure how easy they are, feel free to try:

  1. Changing the refs to something other than any.
  2. Reducing the number of interfaces, I've suggested one, you can open a ticket on this if you don't see other improvements - this might require some infra work.

visible={this.showHint}
animationType="none"
transparent
onBackgroundPress={onBackgroundPress}
onRequestClose={onBackgroundPress}
onRequestClose={onBackgroundPress as () => void}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks like a bug, because the user that sends the onBackgroundPress can expect to receive an event and may rely on it. I think you need to change the prop to:

onBackgroundPress?: (event?: GestureResponderEvent) => void;

(and remove the as () => void)

@M-i-k-e-l M-i-k-e-l assigned M-i-k-e-l and unassigned Inbal-Tish May 4, 2021
@M-i-k-e-l
Copy link
Collaborator

Oh, another thing, the branch name should be something a little more specific (maybe hint-migraion-to-ts), for the next PR :)

@saulve
Copy link
Contributor Author

saulve commented May 5, 2021

@M-i-k-e-l thanks for your time reviewing this. Is there anything else left for me to address?

Copy link
Collaborator

@M-i-k-e-l M-i-k-e-l left a comment

Choose a reason for hiding this comment

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

Nice work, I think there are only two things left, maybe we should talk about the onBackgroundPress

@M-i-k-e-l M-i-k-e-l merged commit 881a259 into master May 5, 2021
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.

3 participants