Move TypeForStyleKey from StyleSheetTypes to StyleSheet

Reviewed By: yungsters

Differential Revision: D7281362

fbshipit-source-id: 936d198e83dfbfc62449e82b5559a30bd57621c4
This commit is contained in:
Eli White 2018-03-20 19:11:54 -07:00 коммит произвёл Facebook Github Bot
Родитель 3b36a6bf06
Коммит f68e04136c
3 изменённых файлов: 42 добавлений и 24 удалений

Просмотреть файл

@ -20,6 +20,7 @@ const flatten = require('flattenStyle');
import type {
____StyleSheetInternalStyleIdentifier_Internal as StyleSheetInternalStyleIdentifier,
____Styles_Internal,
____DangerouslyImpreciseStyle_Internal,
____DangerouslyImpreciseStyleProp_Internal,
____ViewStyleProp_Internal,
____TextStyleProp_Internal,
@ -66,6 +67,40 @@ export type ImageStyleProp = ____ImageStyleProp_Internal;
*/
export type DangerouslyImpreciseStyleProp = ____DangerouslyImpreciseStyleProp_Internal;
/**
* Utility type for getting the values for specific style keys.
*
* The following is bad because position is more restrictive than 'string':
* ```
* type Props = {position: string};
* ```
*
* You should use the following instead:
*
* ```
* type Props = {position: TypeForStyleKey<'position'>};
* ```
*
* This will correctly give you the type 'absolute' | 'relative'
*/
export type TypeForStyleKey<
+key: $Keys<____DangerouslyImpreciseStyle_Internal>,
> = $ElementType<____DangerouslyImpreciseStyle_Internal, key>;
/**
* WARNING: You probably shouldn't be using this type. This type is an object
* with all possible style key's and their values. Note that this isn't
* a safe way to type a style prop for a component as results from
* StyleSheet.create return an internal identifier, not an object of styles.
*
* If you want to type the style prop of a function, consider using
* ViewStyleProp, TextStyleProp, or ImageStyleProp.
*
* This should only be used by very core utilities that operate on an object
* containing any possible style value.
*/
export type DangerouslyImpreciseStyle = ____DangerouslyImpreciseStyle_Internal;
let hairlineWidth = PixelRatio.roundToNearestPixel(0.4);
if (hairlineWidth === 0) {
hairlineWidth = 1 / PixelRatio.get();

Просмотреть файл

@ -213,7 +213,7 @@ export type ImageStyle = $ReadOnly<{|
overlayColor?: string,
|}>;
export type DangerouslyImpreciseStyle = {
export type ____DangerouslyImpreciseStyle_Internal = {
...$Exact<TextStyle>,
+resizeMode?: 'contain' | 'cover' | 'stretch' | 'center' | 'repeat',
+tintColor?: ColorValue,
@ -231,7 +231,7 @@ type GenericStyleProp<+T> =
| $ReadOnlyArray<GenericStyleProp<T>>;
export type ____DangerouslyImpreciseStyleProp_Internal = GenericStyleProp<
$Shape<DangerouslyImpreciseStyle>,
$Shape<____DangerouslyImpreciseStyle_Internal>,
>;
export type ____ViewStyleProp_Internal = GenericStyleProp<
$ReadOnly<$Shape<ViewStyle>>,
@ -244,24 +244,5 @@ export type ____ImageStyleProp_Internal = GenericStyleProp<
>;
export type ____Styles_Internal = {
+[key: string]: $Shape<DangerouslyImpreciseStyle>,
+[key: string]: $Shape<____DangerouslyImpreciseStyle_Internal>,
};
/*
Utility type get non-nullable types for specific style keys.
Useful when a component requires values for certain Style Keys.
So Instead:
```
type Props = {position: string};
```
You should use:
```
type Props = {position: TypeForStyleKey<'position'>};
```
This will correctly give you the type 'absolute' | 'relative' instead of the
weak type of just string;
*/
export type TypeForStyleKey<
+key: $Keys<DangerouslyImpreciseStyle>,
> = $ElementType<DangerouslyImpreciseStyle, key>;

Просмотреть файл

@ -12,8 +12,10 @@
var ReactNativePropRegistry;
import type {DangerouslyImpreciseStyleProp} from 'StyleSheet';
import type {DangerouslyImpreciseStyle} from 'StyleSheetTypes';
import type {
DangerouslyImpreciseStyle,
DangerouslyImpreciseStyleProp,
} from 'StyleSheet';
function getStyle(style) {
if (ReactNativePropRegistry === undefined) {