Skip to content

Commit 24ef22b

Browse files
SamChou19815facebook-github-bot
authored andcommitted
Adopt conditional type for useRefetchableFragment related functions
Reviewed By: panagosg7 Differential Revision: D51774148 fbshipit-source-id: 1e76ea0f4cad928d6bef3f0d1a40994aa9b8544b
1 parent c618cdc commit 24ef22b

5 files changed

+37
-51
lines changed

packages/react-relay/relay-hooks/experimental/useRefetchableFragmentInternal_EXPERIMENTAL.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,13 @@ export type RefetchFn<
5555
// /nullable/.
5656
// - Or, expects /a subset/ of the query variables if the provided key type is
5757
// /non-null/.
58-
// prettier-ignore
5958
export type RefetchFnDynamic<
6059
TQuery: OperationType,
61-
TKey: ?{ +$data?: mixed, ... },
60+
TKey: ?{+$data?: mixed, ...},
6261
TOptions = Options,
63-
> = $Call<
64-
& (( { +$data?: mixed, ... }) => RefetchFnInexact<TQuery, TOptions>)
65-
& ((?{ +$data?: mixed, ... }) => RefetchFnExact<TQuery, TOptions>),
66-
TKey
67-
>;
62+
> = [TKey] extends [{+$data?: mixed, ...}]
63+
? RefetchFnInexact<TQuery, TOptions>
64+
: RefetchFnExact<TQuery, TOptions>;
6865

6966
export type ReturnType<
7067
TQuery: OperationType,
@@ -353,6 +350,7 @@ function useRefetchableFragmentNode<
353350
return {
354351
fragmentData,
355352
fragmentRef,
353+
// $FlowFixMe[incompatible-return] RefetchFn not compatible with RefetchFnDynamic
356354
refetch,
357355
};
358356
}

packages/react-relay/relay-hooks/useBlockingPaginationFragment.js

+9-14
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,12 @@ const {
3434
} = require('relay-runtime');
3535

3636
type RefetchVariables<TVariables, TKey> =
37-
// NOTE: This $Call ensures that the type of the variables is either:
37+
// NOTE: This type ensures that the type of the variables is either:
3838
// - nullable if the provided ref type is non-nullable
3939
// - non-nullable if the provided ref type is nullable, and the caller need to provide the full set of variables
40-
// prettier-ignore
41-
$Call<
42-
& (<TFragmentType>( { +$fragmentSpreads: TFragmentType, ... }) => Partial<TVariables>)
43-
& (<TFragmentType>(?{ +$fragmentSpreads: TFragmentType, ... }) => TVariables),
44-
TKey,
45-
>;
40+
[+key: TKey] extends [+key: {+$fragmentSpreads: mixed, ...}]
41+
? Partial<TVariables>
42+
: TVariables;
4643

4744
type RefetchFnBase<TVars, TOptions> = (
4845
vars: TVars,
@@ -55,15 +52,12 @@ type RefetchFn<TVariables, TKey, TOptions = Options> = RefetchFnBase<
5552
>;
5653

5754
type ReturnType<TVariables, TData, TKey> = {
58-
// NOTE: This $Call ensures that the type of the returned data is either:
55+
// NOTE: This rtpw ensures that the type of the returned data is either:
5956
// - nullable if the provided ref type is nullable
6057
// - non-nullable if the provided ref type is non-nullable
61-
// prettier-ignore
62-
data: $Call<
63-
& (<TFragmentType>( { +$fragmentSpreads: TFragmentType, ... }) => TData)
64-
& (<TFragmentType>(?{ +$fragmentSpreads: TFragmentType, ... }) => ?TData),
65-
TKey,
66-
>,
58+
data: [+key: TKey] extends [+key: {+$fragmentSpreads: mixed, ...}]
59+
? TData
60+
: ?TData,
6761
loadNext: LoadMoreFn<TVariables>,
6862
loadPrevious: LoadMoreFn<TVariables>,
6963
hasNext: boolean,
@@ -162,6 +156,7 @@ function useBlockingPaginationFragment<
162156

163157
return {
164158
// $FlowFixMe[incompatible-cast]
159+
// $FlowFixMe[incompatible-return]
165160
data: (fragmentData: TData),
166161
loadNext,
167162
loadPrevious,

packages/react-relay/relay-hooks/usePaginationFragment.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,12 @@ function useLoadMore<TVariables: Variables>(
157157
}
158158

159159
export type ReturnType<TVariables, TData, TKey> = {
160-
// NOTE: This $Call ensures that the type of the returned data is either:
160+
// NOTE: This type ensures that the type of the returned data is either:
161161
// - nullable if the provided ref type is nullable
162162
// - non-nullable if the provided ref type is non-nullable
163-
// prettier-ignore
164-
data: $Call<
165-
& (<TFragmentType>( { +$fragmentSpreads: TFragmentType, ... }) => TData)
166-
& (<TFragmentType>(?{ +$fragmentSpreads: TFragmentType, ... }) => ?TData),
167-
TKey,
168-
>,
163+
data: [+key: TKey] extends [+key: {+$fragmentSpreads: mixed, ...}]
164+
? TData
165+
: ?TData,
169166
loadNext: LoadMoreFn<TVariables>,
170167
loadPrevious: LoadMoreFn<TVariables>,
171168
hasNext: boolean,
@@ -186,6 +183,7 @@ function usePaginationFragment<
186183
): ReturnType<TVariables, TData, TKey> {
187184
const impl = HooksImplementation.get();
188185
if (impl) {
186+
// $FlowExpectedError[incompatible-return] Flow cannot prove that two conditional type satisfy each other
189187
return impl.usePaginationFragment<TFragmentType, TVariables, TData, TKey>(
190188
fragmentInput,
191189
parentFragmentRef,

packages/react-relay/relay-hooks/useRefetchableFragment.js

+13-16
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,13 @@ const useStaticFragmentNodeWarning = require('./useStaticFragmentNodeWarning');
2525
const {useDebugValue} = require('react');
2626
const {getFragment} = require('relay-runtime');
2727

28-
type RefetchVariables<TVariables, TKey> =
29-
// NOTE: This $Call ensures that the type of the returned variables is either:
28+
type RefetchVariables<TVariables, TKey: ?{+$fragmentSpreads: mixed, ...}> =
29+
// NOTE: This type ensures that the type of the returned variables is either:
3030
// - nullable if the provided ref type is nullable
3131
// - non-nullable if the provided ref type is non-nullable
32-
// prettier-ignore
33-
$Call<
34-
& (<TFragmentType>( { +$fragmentSpreads: TFragmentType, ... }) => Partial<TVariables>)
35-
& (<TFragmentType>(?{ +$fragmentSpreads: TFragmentType, ... }) => TVariables),
36-
TKey,
37-
>;
32+
[+key: TKey] extends [+key: {+$fragmentSpreads: mixed, ...}]
33+
? Partial<TVariables>
34+
: TVariables;
3835

3936
type RefetchFnBase<TVars, TOptions> = (
4037
vars: TVars,
@@ -46,16 +43,15 @@ export type RefetchFn<TVariables, TKey, TOptions = Options> = RefetchFnBase<
4643
TOptions,
4744
>;
4845

49-
export type ReturnType<TVariables, TData, TKey> = [
50-
// NOTE: This $Call ensures that the type of the returned data is either:
46+
export type ReturnType<
47+
TVariables,
48+
TData,
49+
TKey: ?{+$fragmentSpreads: mixed, ...},
50+
> = [
51+
// NOTE: This type ensures that the type of the returned data is either:
5152
// - nullable if the provided ref type is nullable
5253
// - non-nullable if the provided ref type is non-nullable
53-
// prettier-ignore
54-
$Call<
55-
& (<TFragmentType>( { +$fragmentSpreads: TFragmentType, ... }) => TData)
56-
& (<TFragmentType>(?{ +$fragmentSpreads: TFragmentType, ... }) => ?TData),
57-
TKey,
58-
>,
54+
[+key: TKey] extends [+key: {+$fragmentSpreads: mixed, ...}] ? TData : ?TData,
5955
RefetchFn<TVariables, TKey>,
6056
];
6157

@@ -115,6 +111,7 @@ function useRefetchableFragment<
115111
): ReturnType<TVariables, TData, TKey> {
116112
const impl = HooksImplementation.get();
117113
if (impl) {
114+
// $FlowExpectedError[incompatible-return] Flow cannot prove that two conditional type satisfy each other
118115
return impl.useRefetchableFragment<TFragmentType, TVariables, TData, TKey>(
119116
fragmentInput,
120117
parentFragmentRef,

packages/react-relay/relay-hooks/useRefetchableFragmentNode.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,13 @@ export type RefetchFn<
5555
// /nullable/.
5656
// - Or, expects /a subset/ of the query variables if the provided key type is
5757
// /non-null/.
58-
// prettier-ignore
5958
export type RefetchFnDynamic<
6059
TQuery: OperationType,
61-
TKey: ?{ +$data?: mixed, ... },
60+
TKey: ?{+$data?: mixed, ...},
6261
TOptions = Options,
63-
> = $Call<
64-
& (( { +$data?: mixed, ... }) => RefetchFnInexact<TQuery, TOptions>)
65-
& ((?{ +$data?: mixed, ... }) => RefetchFnExact<TQuery, TOptions>),
66-
TKey
67-
>;
62+
> = [TKey] extends [{+$data?: mixed, ...}]
63+
? RefetchFnInexact<TQuery, TOptions>
64+
: RefetchFnExact<TQuery, TOptions>;
6865

6966
export type ReturnType<
7067
TQuery: OperationType,
@@ -363,6 +360,7 @@ function useRefetchableFragmentNode<
363360
return {
364361
fragmentData,
365362
fragmentRef,
363+
// $FlowFixMe[incompatible-return] RefetchFn not compatible with RefetchFnDynamic
366364
refetch,
367365
disableStoreUpdates,
368366
enableStoreUpdates,

0 commit comments

Comments
 (0)