Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions types/react-native/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7000,12 +7000,17 @@ export type BackPressEventName = 'hardwareBackPress';
* Detect hardware back button presses, and programmatically invoke the
* default back button functionality to exit the app if there are no
* listeners or if none of the listeners return true.
* Methods don't have more detailed documentation as of 0.25.
* The event subscriptions are called in reverse order
* (i.e. last registered subscription first), and if one subscription
* returns true then subscriptions registered earlier
* will not be called.
*
* @see https://reactnative.dev/docs/backhandler.html
*/
export interface BackHandlerStatic {
exitApp(): void;
addEventListener(eventName: BackPressEventName, handler: () => void): NativeEventSubscription;
removeEventListener(eventName: BackPressEventName, handler: () => void): void;
addEventListener(eventName: BackPressEventName, handler: () => true | void): NativeEventSubscription;
removeEventListener(eventName: BackPressEventName, handler: () => true | void): void;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This change seems incorrect.. Even without this change, you should be able to pass handle that returns true

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, indeed, one is already able to pass a handler that returns anything at all, as no return value is strictly needed. However the handler must return true, as per documentation (or actually any truthy value, as per implementation), to cause specific behaviour - stopping all the subsequent listeners, including the default one, from executing. And it seems appropriate to document that the handler "should" return true or nothing (even though, in the implementation, the return value is typecasted to boolean, and any value is possible).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The Flow version doesn’t really say anything about return type. Looking at the docs and the implementation, it seems to me like this should just be () => boolean, but perhaps () => boolean | null | undefined is acceptable too.

In any case, it would be good to send a similar change upstream, unless I’m overlooking a reason for this to not be typed as such already. /cc @TheSavior

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I’m not familiar with this. If the flow code doesn’t include boolean then it is likely that we don’t have any callsites doing that, which makes me not trust the documentation. Can you verify the runtime behavior of the listeners and behavior of returning true?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@elicwhite elicwhite Feb 26, 2020

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, okay, I understand better now. The flow code for those functions is untyped. Function is the same as any. I think having the type be boolean | void is reasonable. Please upstream a PR making it typed as returning ?boolean.

@alloy alloy Feb 27, 2020

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think having the type be boolean | void is reasonable. Please upstream a PR making it typed as returning ?boolean.

Just to be sure we’re all on the same page; I assume you meant the Flow equivalent that I posted: () => boolean | null | undefined. Using void would I think send the wrong message, i.e. that the return value would not lead to effects.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Correct. I was agreeing with what you posted :-) Thanks for checking

}

export interface ButtonProps {
Expand Down
1 change: 1 addition & 0 deletions types/react-native/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ function TextUseWindowDimensions() {
}

BackHandler.addEventListener("hardwareBackPress", () => {}).remove();
BackHandler.addEventListener("hardwareBackPress", () => true).remove();

interface LocalStyles {
container: ViewStyle;
Expand Down