Skip to content

Commit e7c44b2

Browse files
j-piaseckifacebook-github-bot
authored andcommitted
Add depth limit to StyleSheet.flatten type (#49925)
Summary: Pull Request resolved: #49925 Changelog: [Internal] Reviewed By: huntie Differential Revision: D70888318 fbshipit-source-id: ded13e64bde87803ce79a8c9272bcb5b6f4ab321
1 parent d818172 commit e7c44b2

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

packages/react-native/Libraries/StyleSheet/StyleSheetTypes.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -1003,9 +1003,26 @@ export type ____Styles_Internal = {
10031003
...
10041004
};
10051005

1006-
export type ____FlattenStyleProp_Internal<+TStyleProp: StyleProp<mixed>> =
1007-
TStyleProp extends null | void | false | ''
1006+
// A depth limiter, to avoid TS2589 in TypeScript. This and
1007+
// ____FlattenStyleProp_Helper should be considered internal.
1008+
type FlattenDepthLimiter = [void, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
1009+
type ____FlattenStyleProp_Helper<
1010+
+TStyleProp: StyleProp<mixed>,
1011+
Depth: $Values<FlattenDepthLimiter> = 9,
1012+
> = Depth extends 0
1013+
? empty
1014+
: TStyleProp extends null | void | false | ''
10081015
? empty
1009-
: TStyleProp extends $ReadOnlyArray<infer V>
1010-
? ____FlattenStyleProp_Internal<V>
1016+
: // When TStyleProp is an array, recurse with decremented Depth
1017+
TStyleProp extends $ReadOnlyArray<infer V>
1018+
? ____FlattenStyleProp_Helper<
1019+
V,
1020+
Depth extends number ? FlattenDepthLimiter[Depth] : 0,
1021+
>
10111022
: TStyleProp;
1023+
1024+
export type ____FlattenStyleProp_Internal<+TStyleProp: StyleProp<mixed>> =
1025+
____FlattenStyleProp_Helper<TStyleProp> extends empty
1026+
? // $FlowFixMe[unclear-type]
1027+
any
1028+
: ____FlattenStyleProp_Helper<TStyleProp>;

packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap

+15-3
Original file line numberDiff line numberDiff line change
@@ -7849,12 +7849,24 @@ export type ____Styles_Internal = {
78497849
+[key: string]: Partial<____DangerouslyImpreciseStyle_Internal>,
78507850
...
78517851
};
7852-
export type ____FlattenStyleProp_Internal<+TStyleProp: StyleProp<mixed>> =
7853-
TStyleProp extends null | void | false | \\"\\"
7852+
type FlattenDepthLimiter = [void, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
7853+
type ____FlattenStyleProp_Helper<
7854+
+TStyleProp: StyleProp<mixed>,
7855+
Depth: $Values<FlattenDepthLimiter> = 9,
7856+
> = Depth extends 0
7857+
? empty
7858+
: TStyleProp extends null | void | false | \\"\\"
78547859
? empty
78557860
: TStyleProp extends $ReadOnlyArray<infer V>
7856-
? ____FlattenStyleProp_Internal<V>
7861+
? ____FlattenStyleProp_Helper<
7862+
V,
7863+
Depth extends number ? FlattenDepthLimiter[Depth] : 0,
7864+
>
78577865
: TStyleProp;
7866+
export type ____FlattenStyleProp_Internal<+TStyleProp: StyleProp<mixed>> =
7867+
____FlattenStyleProp_Helper<TStyleProp> extends empty
7868+
? any
7869+
: ____FlattenStyleProp_Helper<TStyleProp>;
78587870
"
78597871
`;
78607872

0 commit comments

Comments
 (0)