Skip to content
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

Update RN Share.share()'s argument types to be more explicit #44887

Closed
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
26 changes: 16 additions & 10 deletions packages/react-native/Libraries/Share/Share.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import {ColorValue} from '../StyleSheet/StyleSheet';
export type ShareContent =
| {
title?: string | undefined;
message: string;
url: string;
message?: string | undefined;
}
| {
title?: string | undefined;
url: string;
url?: string | undefined;
message: string;
};

export type ShareOptions = {
Expand All @@ -40,29 +42,33 @@ export interface ShareStatic {
* If the user dismissed the dialog, the Promise will still be resolved with action being `Share.dismissedAction`
* and all the other keys being undefined.
*
* In Android, Returns a Promise which always be resolved with action being `Share.sharedAction`.
* In Android, Returns a Promise which always resolves with action being `Share.sharedAction`.
*
* ### Content
*
* #### iOS
*
* - `url` - a URL to share
* - `message` - a message to share
* - `title` - title of the message
*
* #### iOS
* At least one of `URL` or `message` is required.
*
* - `url` - an URL to share
* #### Android
*
* At least one of URL and message is required.
* - `title` - title of the message (optional)
* - `message` - a message to share (often will include a URL).
*
* ### Options
*
* #### iOS
*
* - `excludedActivityTypes`
* - `tintColor`
* - `subject` - a subject to share via email
* - `excludedActivityTypes`
* - `tintColor`
*
* #### Android
*
* - `dialogTitle`
* - `dialogTitle`
*
*/
share(content: ShareContent, options?: ShareOptions): Promise<ShareAction>;
Expand Down
29 changes: 14 additions & 15 deletions packages/react-native/Libraries/Share/Share.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,23 @@ const processColor = require('../StyleSheet/processColor').default;
const Platform = require('../Utilities/Platform');
const invariant = require('invariant');

type Content =
export type ShareContent =
| {
title?: string,
message: string,
...
url: string,
message?: string,
}
| {
title?: string,
url: string,
...
url?: string,
message: string,
};
type Options = {
export type ShareOptions = {
dialogTitle?: string,
excludedActivityTypes?: Array<string>,
tintColor?: string,
subject?: string,
anchor?: number,
...
};

class Share {
Expand All @@ -43,21 +42,21 @@ class Share {
* If the user dismissed the dialog, the Promise will still be resolved with action being `Share.dismissedAction`
* and all the other keys being undefined.
*
* In Android, Returns a Promise which always be resolved with action being `Share.sharedAction`.
* In Android, Returns a Promise which always resolves with action being `Share.sharedAction`.
*
* ### Content
*
* - `message` - a message to share
*
* #### iOS
*
* - `url` - a URL to share
* - `message` - a message to share
*
* At least one of URL and message is required.
* At least one of `URL` or `message` is required.
*
* #### Android
*
* - `title` - title of the message
* - `title` - title of the message (optional)
* - `message` - a message to share (often will include a URL).
*
* ### Options
*
Expand All @@ -73,16 +72,16 @@ class Share {
*
*/
static share(
content: Content,
options: Options = {},
content: ShareContent,
options: ShareOptions = {},
): Promise<{action: string, activityType: ?string}> {
invariant(
typeof content === 'object' && content !== null,
'Content to share must be a valid object',
);
invariant(
typeof content.url === 'string' || typeof content.message === 'string',
'At least one of URL and message is required',
'At least one of URL or message is required',
);
invariant(
typeof options === 'object' && options !== null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7340,29 +7340,28 @@ declare export default typeof NativeShareModule;
`;

exports[`public API should not change unintentionally Libraries/Share/Share.js 1`] = `
"type Content =
"export type ShareContent =
| {
title?: string,
message: string,
...
url: string,
message?: string,
}
| {
title?: string,
url: string,
...
url?: string,
message: string,
};
type Options = {
export type ShareOptions = {
dialogTitle?: string,
excludedActivityTypes?: Array<string>,
tintColor?: string,
subject?: string,
anchor?: number,
...
};
declare class Share {
static share(
content: Content,
options: Options
content: ShareContent,
options: ShareOptions
): Promise<{ action: string, activityType: ?string }>;
static sharedAction: \\"sharedAction\\";
static dismissedAction: \\"dismissedAction\\";
Expand Down