@react-navigation/core 8.0.0-alpha.11 → 8.0.0-alpha.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.tsx CHANGED
@@ -181,7 +181,7 @@ export type EventMapBase = Record<
181
181
  { data?: any; canPreventDefault?: boolean | undefined }
182
182
  >;
183
183
 
184
- export type EventMapCore<State extends NavigationState> = {
184
+ export type EventMapCore<in out State extends NavigationState> = {
185
185
  focus: { data: undefined };
186
186
  blur: { data: undefined };
187
187
  state: { data: { state: State } };
@@ -218,9 +218,9 @@ export type EventArg<
218
218
  : { readonly data: Readonly<Data> });
219
219
 
220
220
  export type EventListenerCallback<
221
- EventMap extends EventMapBase,
222
- EventName extends keyof EventMap,
223
- EventCanPreventDefault extends boolean | undefined =
221
+ in out EventMap extends EventMapBase,
222
+ in out EventName extends keyof EventMap,
223
+ in out EventCanPreventDefault extends boolean | undefined =
224
224
  EventMap[EventName]['canPreventDefault'],
225
225
  > = (
226
226
  e: EventArg<
@@ -230,7 +230,7 @@ export type EventListenerCallback<
230
230
  >
231
231
  ) => void;
232
232
 
233
- export type EventConsumer<EventMap extends EventMapBase> = {
233
+ export type EventConsumer<in out EventMap extends EventMapBase> = {
234
234
  /**
235
235
  * Subscribe to events from the parent navigator.
236
236
  *
@@ -247,7 +247,7 @@ export type EventConsumer<EventMap extends EventMapBase> = {
247
247
  ): void;
248
248
  };
249
249
 
250
- export type EventEmitter<EventMap extends EventMapBase> = {
250
+ export type EventEmitter<in out EventMap extends EventMapBase> = {
251
251
  /**
252
252
  * Emit an event to child screens.
253
253
  *
@@ -436,8 +436,8 @@ type ParamType<
436
436
  : unknown;
437
437
 
438
438
  type NavigationHelpersRoute<
439
- ParamList extends {},
440
- RouteName extends keyof ParamList = KeyOf<ParamList>,
439
+ in out ParamList extends {},
440
+ in out RouteName extends keyof ParamList = KeyOf<ParamList>,
441
441
  > = {
442
442
  /**
443
443
  * Update the param object for the route.
@@ -541,37 +541,45 @@ export type NavigationProp<
541
541
  PrivateValueStore<[ParamList, RouteName, EventMap]>;
542
542
 
543
543
  export type RouteProp<
544
- ParamList extends ParamListBase,
545
- RouteName extends keyof ParamList = KeyOf<ParamList>,
544
+ in out ParamList extends ParamListBase,
545
+ in out RouteName extends keyof ParamList = KeyOf<ParamList>,
546
546
  > = Route<Extract<RouteName, string>, ParamList[RouteName]>;
547
547
 
548
548
  export type CompositeNavigationProp<
549
549
  A extends NavigationProp<ParamListBase, any, any, any, any>,
550
550
  B extends NavigationProp<ParamListBase, any, any, any, any>,
551
+ > = CompositeNavigationPropInternal<
552
+ A,
553
+ B,
554
+ /**
555
+ * Param list from both navigation objects needs to be combined
556
+ * For example, we should be able to navigate to screens in both A and B
557
+ */
558
+ ParamListOfNavigationProp<A> & ParamListOfNavigationProp<B>,
559
+ /**
560
+ * The route name should refer to the route name specified in the first type
561
+ */
562
+ RouteNameOfNavigationProp<A>,
563
+ /**
564
+ * Event map should refer to the config specified in the first type
565
+ */
566
+ EventMapOfNavigationProp<A>
567
+ >;
568
+
569
+ type CompositeNavigationPropInternal<
570
+ A extends NavigationProp<ParamListBase, any, any, any, any>,
571
+ B extends NavigationProp<ParamListBase, any, any, any, any>,
572
+ ParamList extends {},
573
+ RouteName extends keyof ParamList,
574
+ EventMap extends EventMapBase,
551
575
  > = Omit<A & B, keyof NavigationProp<any, any, any, any, any>> &
552
576
  Omit<
553
577
  NavigationProp<
554
- /**
555
- * Param list from both navigation objects needs to be combined
556
- * For example, we should be able to navigate to screens in both A and B
557
- */
558
- ParamListOfNavigationProp<A> & ParamListOfNavigationProp<B>,
559
- /**
560
- * The route name should refer to the route name specified in the first type
561
- */
562
- RouteNameOfNavigationProp<A>,
563
- /**
564
- * The type of state should refer to the state specified in the first type
565
- */
578
+ ParamList,
579
+ RouteName,
566
580
  StateOfNavigationProp<A>,
567
- /**
568
- * Screen options should refer to the options specified in the first type
569
- */
570
581
  ScreenOptionsOfNavigationProp<A>,
571
- /**
572
- * Event map should refer to the config specified in the first type
573
- */
574
- EventMapOfNavigationProp<A>
582
+ EventMap
575
583
  >,
576
584
  'getParent'
577
585
  > & {
@@ -579,13 +587,7 @@ export type CompositeNavigationProp<
579
587
  } & // Mapped types don't preserve protected members
580
588
  // So `Omit` drops `PrivateValueStore`'s `protected` brand
581
589
  // We add it back so this can be used for type inference
582
- PrivateValueStore<
583
- [
584
- ParamListOfNavigationProp<A> & ParamListOfNavigationProp<B>,
585
- RouteNameOfNavigationProp<A>,
586
- EventMapOfNavigationProp<A>,
587
- ]
588
- >;
590
+ PrivateValueStore<[ParamList, RouteName, EventMap]>;
589
591
 
590
592
  type ParamListOfNavigationProp<T> =
591
593
  T extends PrivateValueStore<[infer ParamList, any, any]> ? ParamList : never;
@@ -611,11 +613,11 @@ type EventMapOfNavigationProp<T> =
611
613
  T extends PrivateValueStore<[any, any, infer EventMap]> ? EventMap : {};
612
614
 
613
615
  export type CompositeScreenProps<
614
- A extends {
616
+ in out A extends {
615
617
  navigation: NavigationProp<ParamListBase, string, any, any, any>;
616
618
  route: RouteProp<ParamListBase>;
617
619
  },
618
- B extends {
620
+ in out B extends {
619
621
  navigation: NavigationProp<ParamListBase, string, any, any, any>;
620
622
  },
621
623
  > = {
@@ -624,10 +626,10 @@ export type CompositeScreenProps<
624
626
  };
625
627
 
626
628
  export type ScreenLayoutArgs<
627
- ParamList extends ParamListBase,
628
- RouteName extends keyof ParamList,
629
- ScreenOptions extends {},
630
- Navigation,
629
+ in out ParamList extends ParamListBase,
630
+ in out RouteName extends keyof ParamList,
631
+ in out ScreenOptions extends {},
632
+ in out Navigation,
631
633
  > = {
632
634
  route: RouteProp<ParamList, RouteName>;
633
635
  options: ScreenOptions;
@@ -637,9 +639,9 @@ export type ScreenLayoutArgs<
637
639
  };
638
640
 
639
641
  export type Descriptor<
640
- ScreenOptions extends {},
641
- Navigation extends NavigationProp<ParamListBase, any, any, any, any>,
642
- Route extends RouteProp<any, any>,
642
+ out ScreenOptions extends {},
643
+ out Navigation extends NavigationProp<ParamListBase, any, any, any, any>,
644
+ out Route extends RouteProp<any, any>,
643
645
  > = {
644
646
  /**
645
647
  * Render the component associated with this route.
@@ -663,8 +665,8 @@ export type Descriptor<
663
665
  };
664
666
 
665
667
  export type ScreenListeners<
666
- State extends NavigationState,
667
- EventMap extends EventMapBase,
668
+ in out State extends NavigationState,
669
+ in out EventMap extends EventMapBase,
668
670
  > = Partial<{
669
671
  [EventName in keyof (EventMap & EventMapCore<State>)]: EventListenerCallback<
670
672
  EventMap & EventMapCore<State>,
@@ -715,12 +717,12 @@ export type RouteConfigComponent<
715
717
  };
716
718
 
717
719
  export type RouteConfigProps<
718
- ParamList extends ParamListBase,
719
- RouteName extends keyof ParamList,
720
- State extends NavigationState,
721
- ScreenOptions extends {},
722
- EventMap extends EventMapBase,
723
- Navigation,
720
+ in out ParamList extends ParamListBase,
721
+ in out RouteName extends keyof ParamList,
722
+ in out State extends NavigationState,
723
+ in out ScreenOptions extends {},
724
+ in out EventMap extends EventMapBase,
725
+ in out Navigation,
724
726
  > = {
725
727
  /**
726
728
  * Optional key for this screen. This doesn't need to be unique.
@@ -810,9 +812,9 @@ export type RouteConfig<
810
812
  RouteConfigComponent<ParamList, RouteName>;
811
813
 
812
814
  export type RouteGroupConfig<
813
- ParamList extends ParamListBase,
814
- ScreenOptions extends {},
815
- Navigation,
815
+ in out ParamList extends ParamListBase,
816
+ in out ScreenOptions extends {},
817
+ in out Navigation,
816
818
  > = {
817
819
  /**
818
820
  * Optional key for the screens in this group.
@@ -1081,8 +1083,8 @@ type NavigationListForNestedInternal<Navigator> =
1081
1083
  >;
1082
1084
 
1083
1085
  type NavigationListWithComposite<
1084
- Parent extends NavigationProp<any, any, any, any, any>,
1085
- NavigatorList extends Record<string, any>,
1086
+ in out Parent extends NavigationProp<any, any, any, any, any>,
1087
+ in out NavigatorList extends Record<string, any>,
1086
1088
  > = {
1087
1089
  [K in keyof NavigatorList]: CompositeNavigationProp<NavigatorList[K], Parent>;
1088
1090
  };
@@ -1168,18 +1170,41 @@ export type NavigationContainerRefWithCurrent<ParamList extends {}> =
1168
1170
  current: NavigationContainerRef<ParamList> | null;
1169
1171
  };
1170
1172
 
1171
- export type NavigationListBase<ParamList extends ParamListBase> = {
1173
+ export type NavigationListBase<in out ParamList extends ParamListBase> = {
1172
1174
  [RouteName in keyof ParamList]: unknown;
1173
1175
  };
1174
1176
 
1175
- export type NavigatorTypeBagBase = {
1177
+ // Only use action helpers if all values are functions
1178
+ type ActionHelpersOf<T> =
1179
+ T extends Record<string, (...args: any) => void> ? T : {};
1180
+
1181
+ export interface NavigatorTypeBagBase {
1176
1182
  ParamList: {};
1177
1183
  State: NavigationState;
1178
1184
  ScreenOptions: {};
1179
1185
  EventMap: {};
1180
- NavigationList: NavigationListBase<ParamListBase>;
1186
+ ActionHelpers: {};
1187
+ NavigationList: {
1188
+ [RouteName in keyof this['ParamList']]: NavigationProp<
1189
+ this['ParamList'],
1190
+ RouteName,
1191
+ this['State'],
1192
+ this['ScreenOptions'],
1193
+ this['EventMap'],
1194
+ ActionHelpersOf<this['ActionHelpers']>
1195
+ >;
1196
+ };
1181
1197
  Navigator: React.ComponentType<any>;
1182
- };
1198
+ }
1199
+
1200
+ /**
1201
+ * Adds a proper `ParamList` to a type bag interface
1202
+ * So it can be used as `this['ParamList']`
1203
+ */
1204
+ export type NavigatorTypeBagFor<
1205
+ TypeBag extends NavigatorTypeBagBase,
1206
+ ParamList extends {},
1207
+ > = TypeBag & { ParamList: ParamList };
1183
1208
 
1184
1209
  type TypedNavigatorComponent<Bag extends NavigatorTypeBagBase> =
1185
1210
  React.ComponentType<
@@ -1203,7 +1228,10 @@ type TypedNavigatorStaticDecorated<Bag extends NavigatorTypeBagBase, Config> = {
1203
1228
  config: Config;
1204
1229
  } & PrivateValueStore<[Bag['ParamList'], Bag['NavigationList'], unknown]>;
1205
1230
 
1206
- type TypedNavigatorStatic<Bag extends NavigatorTypeBagBase, Config> = {
1231
+ type TypedNavigatorStatic<
1232
+ in out Bag extends NavigatorTypeBagBase,
1233
+ in out Config,
1234
+ > = {
1207
1235
  config: Config;
1208
1236
  with: (
1209
1237
  Component: React.ComponentType<{
@@ -1229,12 +1257,12 @@ export type TypedNavigator<
1229
1257
  PrivateValueStore<[Bag['ParamList'], Bag['NavigationList'], unknown]>;
1230
1258
 
1231
1259
  type TypedNavigatorInternal<
1232
- ParamList extends ParamListBase,
1233
- State extends NavigationState,
1234
- ScreenOptions extends {},
1235
- EventMap extends EventMapBase,
1236
- NavigationList extends NavigationListBase<ParamList>,
1237
- Navigator extends React.ComponentType<any>,
1260
+ in out ParamList extends ParamListBase,
1261
+ in out State extends NavigationState,
1262
+ in out ScreenOptions extends {},
1263
+ in out EventMap extends EventMapBase,
1264
+ in out NavigationList extends NavigationListBase<ParamList>,
1265
+ in out Navigator extends React.ComponentType<any>,
1238
1266
  > = {
1239
1267
  /**
1240
1268
  * Navigator component which manages the child screens.
@@ -970,9 +970,7 @@ export function useNavigationBuilder<
970
970
  ScreenOptions,
971
971
  EventMap
972
972
  >({
973
- routes: router.getRoutesFromState
974
- ? router.getRoutesFromState(state)
975
- : state.routes,
973
+ routes: state.routes,
976
974
  screens,
977
975
  navigation,
978
976
  screenOptions,
@@ -51,9 +51,11 @@ export function useNavigationState(...args: unknown[]): unknown {
51
51
 
52
52
  if (typeof args[0] === 'string') {
53
53
  // `useNavigation` uses `use` internally, so it's fine to call it conditionally
54
- // @ts-expect-error we can't specify the type here
55
- // eslint-disable-next-line react-hooks/rules-of-hooks
56
- navigation = useNavigation(args[0]);
54
+ // Cast to a non-generic signature to skip overload resolution - otherwise TS
55
+ // eagerly expands `NavigationListForNested<RootNavigator>` at this call site.
56
+ navigation = (
57
+ useNavigation as (name: string) => NavigationProp<ParamListBase>
58
+ )(args[0]);
57
59
  selector = args[1];
58
60
  } else {
59
61
  selector = args[0];