Skip to content

Add static platform check props #37870

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

Closed
wants to merge 1 commit into from
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
6 changes: 6 additions & 0 deletions packages/react-native/Libraries/Utilities/Platform.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export type PlatformSelectSpec<T> = {
const Platform = {
__constants: null,
OS: 'android',
isAndroid: true,
isIOS: false,
isMacOS: false,
isNative: true,
isWeb: false,
isWindows: false,
Comment on lines +23 to +28
Copy link
Contributor

@Pranav-yadav Pranav-yadav Jun 14, 2023

Choose a reason for hiding this comment

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

This would be awesome!
But, this is kind of "redundant" since:

  1. Android and iOS will use and read from their respective files.
  2. If the platform is Android this would implicitly mean it's not iOS, etc., and vice-versa?
  3. Why would an Android app would need a check for MacOS, native, web, and Win similarly for iOS app?
  4. If we want to support (check) all these platforms then we should create another generic file (interface with "all props optional") that will define/expose them.

WDYT?
P.S.: I may be unaware of the full context here :)

Copy link
Contributor Author

@ryanlntn ryanlntn Jun 14, 2023

Choose a reason for hiding this comment

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

It is certainly redundant. I've just noticed that many RN projects have the same const isAndroid = Platform.OS === 'android' declared and thought there may as well be a utility for devs to express one off platform checks this way offered directly by RN.

// $FlowFixMe[unsafe-getters-setters]
get Version(): number {
// $FlowFixMe[object-this-reference]
Expand Down
6 changes: 6 additions & 0 deletions packages/react-native/Libraries/Utilities/Platform.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ type PlatformConstants = {
};
};
interface PlatformStatic {
isAndroid: boolean;
isIOS: boolean;
isMacOS: boolean;
isNative: boolean;
isWeb: boolean;
isWindows: boolean;
isTV: boolean;
isTesting: boolean;
Version: number | string;
Expand Down
6 changes: 6 additions & 0 deletions packages/react-native/Libraries/Utilities/Platform.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export type PlatformSelectSpec<T> = {
const Platform = {
__constants: null,
OS: 'ios',
isAndroid: false,
isIOS: true,
isMacOS: false,
isNative: true,
isWeb: false,
isWindows: false,
// $FlowFixMe[unsafe-getters-setters]
get Version(): string {
// $FlowFixMe[object-this-reference]
Expand Down