Skip to content

Commit 50a481d

Browse files
yungstersfacebook-github-bot
authored andcommitted
RN: Refine StyleSheet.compose Flow Type
Summary: Refines `StyleSheet.compose` so that subtypes of `DangerouslyImpreciseStyleProp` can flow through the function call without losing their type. This makes it so that if you supply two `ViewStyleProp` types, you will get a `ViewStyleProp` type out of it. Reviewed By: TheSavior Differential Revision: D8851699 fbshipit-source-id: e38e572e363a71fddf63d6b6bf5a96b3cdae5915
1 parent 732c3a4 commit 50a481d

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

Libraries/StyleSheet/StyleSheet.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,12 @@ module.exports = {
272272
* array, saving allocations and maintaining reference equality for
273273
* PureComponent checks.
274274
*/
275-
compose(
276-
style1: ?DangerouslyImpreciseStyleProp,
277-
style2: ?DangerouslyImpreciseStyleProp,
278-
): ?DangerouslyImpreciseStyleProp {
275+
compose<T: DangerouslyImpreciseStyleProp>(
276+
style1: ?T,
277+
style2: ?T,
278+
): ?T | $ReadOnlyArray<T> {
279279
if (style1 != null && style2 != null) {
280-
return [style1, style2];
280+
return ([style1, style2]: $ReadOnlyArray<T>);
281281
} else {
282282
return style1 != null ? style1 : style2;
283283
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
const StyleSheet = require('StyleSheet');
14+
15+
import type {ImageStyleProp, TextStyleProp} from 'StyleSheet';
16+
17+
const imageStyle = {tintColor: 'rgb(0, 0, 0)'};
18+
const textStyle = {color: 'rgb(0, 0, 0)'};
19+
20+
module.exports = {
21+
testGoodCompose() {
22+
(StyleSheet.compose(
23+
imageStyle,
24+
imageStyle,
25+
): ImageStyleProp);
26+
27+
(StyleSheet.compose(
28+
textStyle,
29+
textStyle,
30+
): TextStyleProp);
31+
32+
(StyleSheet.compose(
33+
null,
34+
null,
35+
): TextStyleProp);
36+
37+
(StyleSheet.compose(
38+
textStyle,
39+
null,
40+
): TextStyleProp);
41+
42+
(StyleSheet.compose(
43+
textStyle,
44+
Math.random() < 0.5 ? textStyle : null,
45+
): TextStyleProp);
46+
47+
(StyleSheet.compose(
48+
[textStyle],
49+
null,
50+
): TextStyleProp);
51+
52+
(StyleSheet.compose(
53+
[textStyle],
54+
null,
55+
): TextStyleProp);
56+
57+
(StyleSheet.compose(
58+
[textStyle],
59+
[textStyle],
60+
): TextStyleProp);
61+
},
62+
63+
testBadCompose() {
64+
// $FlowExpectedError - Incompatible type.
65+
(StyleSheet.compose(
66+
textStyle,
67+
textStyle,
68+
): ImageStyleProp);
69+
70+
(StyleSheet.compose(
71+
// $FlowExpectedError - Incompatible type.
72+
[textStyle],
73+
null,
74+
): ImageStyleProp);
75+
76+
// $FlowExpectedError - Incompatible type.
77+
(StyleSheet.compose(
78+
Math.random() < 0.5 ? textStyle : null,
79+
null,
80+
): ImageStyleProp);
81+
},
82+
};

0 commit comments

Comments
 (0)