From 55fd0262a458edc520c1e9c0f7a1eccaec2072ee Mon Sep 17 00:00:00 2001 From: Sam Zhou Date: Thu, 3 Aug 2023 11:44:13 -0700 Subject: [PATCH] Unify and cleanup Platform Flow typing (#38731) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/38731 Changelog: [Internal] Reviewed By: yungsters Differential Revision: D47965692 fbshipit-source-id: ad61acd28a3362b64fa90f413a876c29c1166132 --- .../DrawerLayoutAndroid.android.js | 4 +- .../Libraries/Utilities/Platform.android.js | 14 ++-- .../Libraries/Utilities/Platform.flow.js | 84 +++++++++++++++++++ .../Libraries/Utilities/Platform.ios.js | 14 ++-- 4 files changed, 99 insertions(+), 17 deletions(-) create mode 100644 packages/react-native/Libraries/Utilities/Platform.flow.js diff --git a/packages/react-native/Libraries/Components/DrawerAndroid/DrawerLayoutAndroid.android.js b/packages/react-native/Libraries/Components/DrawerAndroid/DrawerLayoutAndroid.android.js index d3a722dea6f974..98b754ce8012c7 100644 --- a/packages/react-native/Libraries/Components/DrawerAndroid/DrawerLayoutAndroid.android.js +++ b/packages/react-native/Libraries/Components/DrawerAndroid/DrawerLayoutAndroid.android.js @@ -181,7 +181,9 @@ class DrawerLayoutAndroid extends React.Component { ...props } = this.props; const drawStatusBar = - Platform.Version >= 21 && this.props.statusBarBackgroundColor != null; + Platform.OS === 'android' && + Platform.Version >= 21 && + this.props.statusBarBackgroundColor != null; const drawerViewWrapper = ( = { - android?: T, - native?: T, - default?: T, - ... -}; +import NativePlatformConstantsAndroid from './NativePlatformConstantsAndroid'; -const Platform = { +const Platform: PlatformType = { __constants: null, OS: 'android', // $FlowFixMe[unsafe-getters-setters] diff --git a/packages/react-native/Libraries/Utilities/Platform.flow.js b/packages/react-native/Libraries/Utilities/Platform.flow.js new file mode 100644 index 00000000000000..d4bc8147249e36 --- /dev/null +++ b/packages/react-native/Libraries/Utilities/Platform.flow.js @@ -0,0 +1,84 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict + * @format + */ + +export type PlatformSelectSpec = { + default?: T, + native?: T, + ios?: T, + android?: T, + ... +}; + +type IOSPlatform = { + __constants: null, + OS: $TEMPORARY$string<'ios'>, + // $FlowFixMe[unsafe-getters-setters] + get Version(): string, + // $FlowFixMe[unsafe-getters-setters] + get constants(): {| + forceTouchAvailable: boolean, + interfaceIdiom: string, + isTesting: boolean, + isDisableAnimations?: boolean, + osVersion: string, + reactNativeVersion: {| + major: number, + minor: number, + patch: number, + prerelease: ?number, + |}, + systemName: string, + |}, + // $FlowFixMe[unsafe-getters-setters] + get isPad(): boolean, + // $FlowFixMe[unsafe-getters-setters] + get isTV(): boolean, + // $FlowFixMe[unsafe-getters-setters] + get isTesting(): boolean, + // $FlowFixMe[unsafe-getters-setters] + get isDisableAnimations(): boolean, + select: (spec: PlatformSelectSpec) => T, +}; + +type AndroidPlatform = { + __constants: null, + OS: $TEMPORARY$string<'android'>, + // $FlowFixMe[unsafe-getters-setters] + get Version(): number, + // $FlowFixMe[unsafe-getters-setters] + get constants(): {| + isTesting: boolean, + isDisableAnimations?: boolean, + reactNativeVersion: {| + major: number, + minor: number, + patch: number, + prerelease: ?number, + |}, + Version: number, + Release: string, + Serial: string, + Fingerprint: string, + Model: string, + ServerHost?: string, + uiMode: string, + Brand: string, + Manufacturer: string, + |}, + // $FlowFixMe[unsafe-getters-setters] + get isTV(): boolean, + // $FlowFixMe[unsafe-getters-setters] + get isTesting(): boolean, + // $FlowFixMe[unsafe-getters-setters] + get isDisableAnimations(): boolean, + select: (spec: PlatformSelectSpec) => T, +}; + +export type Platform = IOSPlatform | AndroidPlatform; diff --git a/packages/react-native/Libraries/Utilities/Platform.ios.js b/packages/react-native/Libraries/Utilities/Platform.ios.js index 4d0217a80945f4..53371634e7d9f7 100644 --- a/packages/react-native/Libraries/Utilities/Platform.ios.js +++ b/packages/react-native/Libraries/Utilities/Platform.ios.js @@ -8,16 +8,14 @@ * @flow strict */ -import NativePlatformConstantsIOS from './NativePlatformConstantsIOS'; +import type { + Platform as PlatformType, + PlatformSelectSpec, +} from './Platform.flow'; -export type PlatformSelectSpec = { - default?: T, - native?: T, - ios?: T, - ... -}; +import NativePlatformConstantsIOS from './NativePlatformConstantsIOS'; -const Platform = { +const Platform: PlatformType = { __constants: null, OS: 'ios', // $FlowFixMe[unsafe-getters-setters]