Skip to content

Commit

Permalink
Use conditional type to flattern RN styles (facebook#41931)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#41931

`GenericStyleProp` is defined as

```
type GenericStyleProp<+T> =
  | null
  | void
  | T
  | false
  | ''
  | $ReadOnlyArray<GenericStyleProp<T>>;
```

and `____FlattenStyleProp_Internal` is designed to reverse it. We can use conditional type to achieve it instead of $Call:

`null | void | false | ''` doesn't contribute to anything doing reversal, so they are mapped to empty. When we encounter $ReadOnlyArray, we recursively apply `____FlattenStyleProp_Internal`. Otherwise, we return the input type.

Changelog: [Internal]

Reviewed By: jbrown215

Differential Revision: D52142082

fbshipit-source-id: 590c71c6400498730675e20c67b173c3bc285d00
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Dec 15, 2023
1 parent d45a01d commit ce69213
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions packages/react-native/Libraries/StyleSheet/StyleSheetTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -912,8 +912,10 @@ export type ____Styles_Internal = {
...
};

// $FlowFixMe[deprecated-type]
export type ____FlattenStyleProp_Internal<+TStyleProp> = $Call<
<T>(GenericStyleProp<T>) => T,
TStyleProp,
>;
export type ____FlattenStyleProp_Internal<
+TStyleProp: GenericStyleProp<mixed>,
> = TStyleProp extends null | void | false | ''
? empty
: TStyleProp extends $ReadOnlyArray<infer V>
? ____FlattenStyleProp_Internal<V>
: TStyleProp;
4 changes: 2 additions & 2 deletions packages/react-native/Libraries/StyleSheet/flattenStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
import type {DangerouslyImpreciseStyleProp} from './StyleSheet';
import type {____FlattenStyleProp_Internal} from './StyleSheetTypes';

// $FlowFixMe[unsupported-variance-annotation]
function flattenStyle<+TStyleProp: DangerouslyImpreciseStyleProp>(
function flattenStyle<TStyleProp: DangerouslyImpreciseStyleProp>(
style: ?TStyleProp,
// $FlowFixMe[underconstrained-implicit-instantiation]
): ?____FlattenStyleProp_Internal<TStyleProp> {
Expand All @@ -23,6 +22,7 @@ function flattenStyle<+TStyleProp: DangerouslyImpreciseStyleProp>(
}

if (!Array.isArray(style)) {
// $FlowFixMe[incompatible-return]
return style;
}

Expand Down

0 comments on commit ce69213

Please sign in to comment.