Skip to content

Commit 6c3f654

Browse files
comp615necolas
authored andcommitted
Further improvements to Flow types
Close #1985
1 parent ab5d119 commit 6c3f654

File tree

9 files changed

+59
-31
lines changed

9 files changed

+59
-31
lines changed

packages/react-native-web/src/exports/AppRegistry/index.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,39 @@
88
* @flow
99
*/
1010

11-
import type { ComponentType } from 'react';
11+
import type { ComponentType, Node } from 'react';
1212

1313
import invariant from 'fbjs/lib/invariant';
1414
import unmountComponentAtNode from '../unmountComponentAtNode';
1515
import renderApplication, { getApplication } from './renderApplication';
1616

17-
const emptyObject = {};
18-
const runnables = {};
17+
type AppParams = Object;
18+
type Runnable = {|
19+
getApplication?: (AppParams) => {| element: Node, getStyleElement: (any) => Node |},
20+
run: (AppParams) => any
21+
|};
1922

2023
export type ComponentProvider = () => ComponentType<any>;
2124
export type ComponentProviderInstrumentationHook = (
2225
component: ComponentProvider
2326
) => ComponentType<any>;
2427
export type WrapperComponentProvider = (any) => ComponentType<*>;
2528

26-
let componentProviderInstrumentationHook: ComponentProviderInstrumentationHook = (
27-
component: ComponentProvider
28-
) => component();
29-
let wrapperComponentProvider: ?WrapperComponentProvider;
30-
3129
export type AppConfig = {
3230
appKey: string,
3331
component?: ComponentProvider,
3432
run?: Function,
3533
section?: boolean
3634
};
3735

36+
const emptyObject = {};
37+
const runnables: {| [appKey: string]: Runnable |} = {};
38+
39+
let componentProviderInstrumentationHook: ComponentProviderInstrumentationHook = (
40+
component: ComponentProvider
41+
) => component();
42+
let wrapperComponentProvider: ?WrapperComponentProvider;
43+
3844
/**
3945
* `AppRegistry` is the JS entry point to running all React Native apps.
4046
*/
@@ -43,7 +49,10 @@ export default class AppRegistry {
4349
return Object.keys(runnables);
4450
}
4551

46-
static getApplication(appKey: string, appParameters?: Object): string {
52+
static getApplication(
53+
appKey: string,
54+
appParameters?: AppParams
55+
): {| element: Node, getStyleElement: (any) => Node |} {
4756
invariant(
4857
runnables[appKey] && runnables[appKey].getApplication,
4958
`Application ${appKey} has not been registered. ` +

packages/react-native-web/src/exports/AppRegistry/renderApplication.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @flow
99
*/
1010

11-
import type { ComponentType } from 'react';
11+
import type { ComponentType, Node } from 'react';
1212

1313
import AppContainer from './AppContainer';
1414
import invariant from 'fbjs/lib/invariant';
@@ -44,7 +44,7 @@ export function getApplication(
4444
RootComponent: ComponentType<Object>,
4545
initialProps: Object,
4646
WrapperComponent?: ?ComponentType<*>
47-
): Object {
47+
): {| element: Node, getStyleElement: (Object) => Node |} {
4848
const element = (
4949
<AppContainer WrapperComponent={WrapperComponent} rootTag={{}}>
5050
<RootComponent {...initialProps} />

packages/react-native-web/src/exports/Dimensions/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export type DisplayMetrics = {|
1919
|};
2020

2121
type DimensionsValue = {|
22-
window?: DisplayMetrics,
23-
screen?: DisplayMetrics
22+
window: DisplayMetrics,
23+
screen: DisplayMetrics
2424
|};
2525

2626
type DimensionKey = 'window' | 'screen';

packages/react-native-web/src/exports/Image/index.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ function resolveAssetUri(source): ?string {
132132
return uri;
133133
}
134134

135+
interface ImageStatics {
136+
getSize: (
137+
uri: string,
138+
success: (width: number, height: number) => void,
139+
failure: () => void
140+
) => void;
141+
prefetch: (uri: string) => Promise<void>;
142+
queryCache: (uris: Array<string>) => Promise<{| [uri: string]: 'disk/memory' |}>;
143+
}
144+
135145
const Image: React.AbstractComponent<ImageProps, React.ElementRef<typeof View>> = React.forwardRef(
136146
(props, ref) => {
137147
const {
@@ -294,18 +304,22 @@ const Image: React.AbstractComponent<ImageProps, React.ElementRef<typeof View>>
294304

295305
Image.displayName = 'Image';
296306

297-
// $FlowFixMe
298-
Image.getSize = function (uri, success, failure) {
307+
// $FlowIgnore: This is the correct type, but casting makes it unhappy since the variables aren't defined yet
308+
const ImageWithStatics = (Image: React.AbstractComponent<
309+
ImageProps,
310+
React.ElementRef<typeof View>
311+
> &
312+
ImageStatics);
313+
314+
ImageWithStatics.getSize = function (uri, success, failure) {
299315
ImageLoader.getSize(uri, success, failure);
300316
};
301317

302-
// $FlowFixMe
303-
Image.prefetch = function (uri) {
318+
ImageWithStatics.prefetch = function (uri) {
304319
return ImageLoader.prefetch(uri);
305320
};
306321

307-
// $FlowFixMe
308-
Image.queryCache = function (uris) {
322+
ImageWithStatics.queryCache = function (uris) {
309323
return ImageLoader.queryCache(uris);
310324
};
311325

@@ -364,4 +378,4 @@ const resizeModeStyles = StyleSheet.create({
364378
}
365379
});
366380

367-
export default Image;
381+
export default ImageWithStatics;

packages/react-native-web/src/exports/Image/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,6 @@ export type ImageProps = {
108108
onLoadStart?: (e: any) => void,
109109
onProgress?: (e: any) => void,
110110
resizeMode?: ResizeMode,
111-
source: Source,
111+
source?: Source,
112112
style?: GenericStyleProp<ImageStyle>
113113
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
// @flow strict
2+
13
import PanResponder from '../../vendor/react-native/PanResponder';
24
export default PanResponder;

packages/react-native-web/src/modules/ImageLoader/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const ImageLoader = {
8282
delete requests[`${requestId}`];
8383
}
8484
},
85-
getSize(uri: string, success: Function, failure: Function) {
85+
getSize(uri: string, success: (width: number, height: number) => void, failure: () => void) {
8686
let complete = false;
8787
const interval = setInterval(callback, 16);
8888
const requestId = ImageLoader.load(uri, callback, errorCallback);
@@ -133,7 +133,7 @@ const ImageLoader = {
133133
requests[`${id}`] = image;
134134
return id;
135135
},
136-
prefetch(uri: string): Promise<*> {
136+
prefetch(uri: string): Promise<void> {
137137
return new Promise((resolve, reject) => {
138138
ImageLoader.load(
139139
uri,
@@ -148,7 +148,7 @@ const ImageLoader = {
148148
);
149149
});
150150
},
151-
queryCache(uris: Array<string>): Object {
151+
queryCache(uris: Array<string>): Promise<{| [uri: string]: 'disk/memory' |}> {
152152
const result = {};
153153
uris.forEach((u) => {
154154
if (ImageUriCache.has(u)) {

packages/react-native-web/src/types/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,16 @@ export type PointValue = {|
4646
y: number
4747
|};
4848

49-
type LayoutCallback = ({
50-
...LayoutValue,
49+
type LayoutCallback = (
50+
x: number,
51+
y: number,
52+
width: number,
53+
height: number,
5154
left: number,
5255
top: number
53-
}) => void;
56+
) => void;
5457

55-
type MeasureInWindowCallback = (EdgeInsetsValue) => void;
58+
type MeasureInWindowCallback = (left: number, top: number, width: number, height: number) => void;
5659

5760
// Mixin to HTMLElement that represents additions from the `usePlatformMethods` hook
5861
export interface PlatformMethods {

packages/react-native-web/src/vendor/react-native/Types/CoreEventTypes.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
export type SyntheticEvent<T> = $ReadOnly<{|
1414
bubbles: ?boolean,
1515
cancelable: ?boolean,
16-
currentTarget: number,
16+
currentTarget: HTMLElement,
1717
defaultPrevented: ?boolean,
1818
dispatchConfig: $ReadOnly<{|
1919
registrationName: string,
@@ -26,7 +26,7 @@ export type SyntheticEvent<T> = $ReadOnly<{|
2626
isTrusted: ?boolean,
2727
nativeEvent: T,
2828
persist: () => void,
29-
target: ?number,
29+
target: ?HTMLElement,
3030
timeStamp: number,
3131
type: ?string,
3232
|}>;
@@ -91,7 +91,7 @@ export type PressEvent = ResponderSyntheticEvent<
9191
locationY: number,
9292
pageX: number,
9393
pageY: number,
94-
target: ?number,
94+
target: ?HTMLElement,
9595
timestamp: number,
9696
touches: $ReadOnlyArray<$PropertyType<PressEvent, 'nativeEvent'>>,
9797
|}>,

0 commit comments

Comments
 (0)