mirror of
https://github.com/status-im/react-native.git
synced 2025-01-14 11:34:23 +00:00
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
This commit is contained in:
parent
732c3a4f4e
commit
50a481d23a
@ -272,12 +272,12 @@ module.exports = {
|
|||||||
* array, saving allocations and maintaining reference equality for
|
* array, saving allocations and maintaining reference equality for
|
||||||
* PureComponent checks.
|
* PureComponent checks.
|
||||||
*/
|
*/
|
||||||
compose(
|
compose<T: DangerouslyImpreciseStyleProp>(
|
||||||
style1: ?DangerouslyImpreciseStyleProp,
|
style1: ?T,
|
||||||
style2: ?DangerouslyImpreciseStyleProp,
|
style2: ?T,
|
||||||
): ?DangerouslyImpreciseStyleProp {
|
): ?T | $ReadOnlyArray<T> {
|
||||||
if (style1 != null && style2 != null) {
|
if (style1 != null && style2 != null) {
|
||||||
return [style1, style2];
|
return ([style1, style2]: $ReadOnlyArray<T>);
|
||||||
} else {
|
} else {
|
||||||
return style1 != null ? style1 : style2;
|
return style1 != null ? style1 : style2;
|
||||||
}
|
}
|
||||||
|
82
Libraries/StyleSheet/__flowtests__/StyleSheet-flowtest.js
Normal file
82
Libraries/StyleSheet/__flowtests__/StyleSheet-flowtest.js
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2015-present, Facebook, Inc.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*
|
||||||
|
* @flow strict-local
|
||||||
|
* @format
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const StyleSheet = require('StyleSheet');
|
||||||
|
|
||||||
|
import type {ImageStyleProp, TextStyleProp} from 'StyleSheet';
|
||||||
|
|
||||||
|
const imageStyle = {tintColor: 'rgb(0, 0, 0)'};
|
||||||
|
const textStyle = {color: 'rgb(0, 0, 0)'};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
testGoodCompose() {
|
||||||
|
(StyleSheet.compose(
|
||||||
|
imageStyle,
|
||||||
|
imageStyle,
|
||||||
|
): ImageStyleProp);
|
||||||
|
|
||||||
|
(StyleSheet.compose(
|
||||||
|
textStyle,
|
||||||
|
textStyle,
|
||||||
|
): TextStyleProp);
|
||||||
|
|
||||||
|
(StyleSheet.compose(
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
): TextStyleProp);
|
||||||
|
|
||||||
|
(StyleSheet.compose(
|
||||||
|
textStyle,
|
||||||
|
null,
|
||||||
|
): TextStyleProp);
|
||||||
|
|
||||||
|
(StyleSheet.compose(
|
||||||
|
textStyle,
|
||||||
|
Math.random() < 0.5 ? textStyle : null,
|
||||||
|
): TextStyleProp);
|
||||||
|
|
||||||
|
(StyleSheet.compose(
|
||||||
|
[textStyle],
|
||||||
|
null,
|
||||||
|
): TextStyleProp);
|
||||||
|
|
||||||
|
(StyleSheet.compose(
|
||||||
|
[textStyle],
|
||||||
|
null,
|
||||||
|
): TextStyleProp);
|
||||||
|
|
||||||
|
(StyleSheet.compose(
|
||||||
|
[textStyle],
|
||||||
|
[textStyle],
|
||||||
|
): TextStyleProp);
|
||||||
|
},
|
||||||
|
|
||||||
|
testBadCompose() {
|
||||||
|
// $FlowExpectedError - Incompatible type.
|
||||||
|
(StyleSheet.compose(
|
||||||
|
textStyle,
|
||||||
|
textStyle,
|
||||||
|
): ImageStyleProp);
|
||||||
|
|
||||||
|
(StyleSheet.compose(
|
||||||
|
// $FlowExpectedError - Incompatible type.
|
||||||
|
[textStyle],
|
||||||
|
null,
|
||||||
|
): ImageStyleProp);
|
||||||
|
|
||||||
|
// $FlowExpectedError - Incompatible type.
|
||||||
|
(StyleSheet.compose(
|
||||||
|
Math.random() < 0.5 ? textStyle : null,
|
||||||
|
null,
|
||||||
|
): ImageStyleProp);
|
||||||
|
},
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user