@react-navigation/core 8.0.0-alpha.2 → 8.0.0-alpha.20

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.
Files changed (251) hide show
  1. package/lib/module/BaseNavigationContainer.js +20 -9
  2. package/lib/module/BaseNavigationContainer.js.map +1 -1
  3. package/lib/module/ConsumedParamsContext.js +5 -0
  4. package/lib/module/ConsumedParamsContext.js.map +1 -0
  5. package/lib/module/NavigationBuilderContext.js +1 -0
  6. package/lib/module/NavigationBuilderContext.js.map +1 -1
  7. package/lib/module/NavigationIndependentTree.js +8 -4
  8. package/lib/module/NavigationIndependentTree.js.map +1 -1
  9. package/lib/module/NavigationProvider.js +14 -3
  10. package/lib/module/NavigationProvider.js.map +1 -1
  11. package/lib/module/NavigationStateContext.js.map +1 -1
  12. package/lib/module/PreventRemoveProvider.js +3 -3
  13. package/lib/module/PreventRemoveProvider.js.map +1 -1
  14. package/lib/module/SceneView.js +46 -56
  15. package/lib/module/SceneView.js.map +1 -1
  16. package/lib/module/StaticNavigation.js +166 -46
  17. package/lib/module/StaticNavigation.js.map +1 -1
  18. package/lib/module/checkSerializable.js +11 -4
  19. package/lib/module/checkSerializable.js.map +1 -1
  20. package/lib/module/createNavigatorFactory.js +27 -5
  21. package/lib/module/createNavigatorFactory.js.map +1 -1
  22. package/lib/module/findFocusedRoute.js +7 -4
  23. package/lib/module/findFocusedRoute.js.map +1 -1
  24. package/lib/module/getActionFromState.js +40 -14
  25. package/lib/module/getActionFromState.js.map +1 -1
  26. package/lib/module/getPathFromState.js +48 -11
  27. package/lib/module/getPathFromState.js.map +1 -1
  28. package/lib/module/getPatternParts.js +3 -0
  29. package/lib/module/getPatternParts.js.map +1 -1
  30. package/lib/module/getStateFromPath.js +353 -166
  31. package/lib/module/getStateFromPath.js.map +1 -1
  32. package/lib/module/getStateFromRouteParams.js +24 -0
  33. package/lib/module/getStateFromRouteParams.js.map +1 -0
  34. package/lib/module/index.js +3 -4
  35. package/lib/module/index.js.map +1 -1
  36. package/lib/module/theming/useTheme.js +1 -1
  37. package/lib/module/theming/useTheme.js.map +1 -1
  38. package/lib/module/types.js +15 -0
  39. package/lib/module/types.js.map +1 -1
  40. package/lib/module/useDescriptors.js +18 -9
  41. package/lib/module/useDescriptors.js.map +1 -1
  42. package/lib/module/useEventEmitter.js +43 -31
  43. package/lib/module/useEventEmitter.js.map +1 -1
  44. package/lib/module/useFocusEvents.js +6 -2
  45. package/lib/module/useFocusEvents.js.map +1 -1
  46. package/lib/module/useFocusedListenersChildrenAdapter.js +2 -2
  47. package/lib/module/useFocusedListenersChildrenAdapter.js.map +1 -1
  48. package/lib/module/useIsFocused.js +7 -12
  49. package/lib/module/useIsFocused.js.map +1 -1
  50. package/lib/module/useNavigation.js.map +1 -1
  51. package/lib/module/useNavigationBuilder.js +144 -86
  52. package/lib/module/useNavigationBuilder.js.map +1 -1
  53. package/lib/module/useNavigationCache.js +3 -3
  54. package/lib/module/useNavigationCache.js.map +1 -1
  55. package/lib/module/useNavigationHelpers.js +1 -1
  56. package/lib/module/useNavigationHelpers.js.map +1 -1
  57. package/lib/module/useNavigationIndependentTree.js +1 -1
  58. package/lib/module/useNavigationIndependentTree.js.map +1 -1
  59. package/lib/module/useNavigationState.js +6 -4
  60. package/lib/module/useNavigationState.js.map +1 -1
  61. package/lib/module/useOnAction.js +3 -3
  62. package/lib/module/useOnAction.js.map +1 -1
  63. package/lib/module/useOnGetState.js +6 -3
  64. package/lib/module/useOnGetState.js.map +1 -1
  65. package/lib/module/useOnPreventRemove.js +3 -3
  66. package/lib/module/useOnPreventRemove.js.map +1 -1
  67. package/lib/module/useOnRouteFocus.js +1 -1
  68. package/lib/module/useOnRouteFocus.js.map +1 -1
  69. package/lib/module/useOptionsGetters.js +7 -4
  70. package/lib/module/useOptionsGetters.js.map +1 -1
  71. package/lib/module/usePreventRemove.js +1 -1
  72. package/lib/module/usePreventRemove.js.map +1 -1
  73. package/lib/module/usePreventRemoveContext.js +1 -1
  74. package/lib/module/usePreventRemoveContext.js.map +1 -1
  75. package/lib/module/useRegisterNavigator.js +2 -2
  76. package/lib/module/useRegisterNavigator.js.map +1 -1
  77. package/lib/module/useRoute.js.map +1 -1
  78. package/lib/module/useRouteCache.js +2 -1
  79. package/lib/module/useRouteCache.js.map +1 -1
  80. package/lib/module/useScheduleUpdate.js +1 -1
  81. package/lib/module/useScheduleUpdate.js.map +1 -1
  82. package/lib/module/useStateForPath.js +1 -1
  83. package/lib/module/useStateForPath.js.map +1 -1
  84. package/lib/module/validatePathConfig.js +2 -1
  85. package/lib/module/validatePathConfig.js.map +1 -1
  86. package/lib/typescript/src/BaseNavigationContainer.d.ts +6 -3
  87. package/lib/typescript/src/BaseNavigationContainer.d.ts.map +1 -1
  88. package/lib/typescript/src/ConsumedParamsContext.d.ts +8 -0
  89. package/lib/typescript/src/ConsumedParamsContext.d.ts.map +1 -0
  90. package/lib/typescript/src/EnsureSingleNavigator.d.ts +1 -1
  91. package/lib/typescript/src/EnsureSingleNavigator.d.ts.map +1 -1
  92. package/lib/typescript/src/Group.d.ts +1 -1
  93. package/lib/typescript/src/Group.d.ts.map +1 -1
  94. package/lib/typescript/src/NavigationBuilderContext.d.ts +12 -6
  95. package/lib/typescript/src/NavigationBuilderContext.d.ts.map +1 -1
  96. package/lib/typescript/src/NavigationContainerRefContext.d.ts +1 -1
  97. package/lib/typescript/src/NavigationContainerRefContext.d.ts.map +1 -1
  98. package/lib/typescript/src/NavigationFocusedRouteStateContext.d.ts +4 -4
  99. package/lib/typescript/src/NavigationFocusedRouteStateContext.d.ts.map +1 -1
  100. package/lib/typescript/src/NavigationHelpersContext.d.ts +1 -1
  101. package/lib/typescript/src/NavigationHelpersContext.d.ts.map +1 -1
  102. package/lib/typescript/src/NavigationIndependentTree.d.ts +1 -1
  103. package/lib/typescript/src/NavigationIndependentTree.d.ts.map +1 -1
  104. package/lib/typescript/src/NavigationProvider.d.ts +10 -10
  105. package/lib/typescript/src/NavigationProvider.d.ts.map +1 -1
  106. package/lib/typescript/src/NavigationStateContext.d.ts +3 -3
  107. package/lib/typescript/src/NavigationStateContext.d.ts.map +1 -1
  108. package/lib/typescript/src/PreventRemoveProvider.d.ts +1 -1
  109. package/lib/typescript/src/PreventRemoveProvider.d.ts.map +1 -1
  110. package/lib/typescript/src/SceneView.d.ts +3 -2
  111. package/lib/typescript/src/SceneView.d.ts.map +1 -1
  112. package/lib/typescript/src/Screen.d.ts +1 -1
  113. package/lib/typescript/src/Screen.d.ts.map +1 -1
  114. package/lib/typescript/src/StaticContainer.d.ts +1 -1
  115. package/lib/typescript/src/StaticContainer.d.ts.map +1 -1
  116. package/lib/typescript/src/StaticNavigation.d.ts +123 -92
  117. package/lib/typescript/src/StaticNavigation.d.ts.map +1 -1
  118. package/lib/typescript/src/checkSerializable.d.ts +5 -3
  119. package/lib/typescript/src/checkSerializable.d.ts.map +1 -1
  120. package/lib/typescript/src/createNavigationContainerRef.d.ts +1 -1
  121. package/lib/typescript/src/createNavigationContainerRef.d.ts.map +1 -1
  122. package/lib/typescript/src/createNavigatorFactory.d.ts +17 -5
  123. package/lib/typescript/src/createNavigatorFactory.d.ts.map +1 -1
  124. package/lib/typescript/src/findFocusedRoute.d.ts +3 -3
  125. package/lib/typescript/src/findFocusedRoute.d.ts.map +1 -1
  126. package/lib/typescript/src/getActionFromState.d.ts +4 -4
  127. package/lib/typescript/src/getActionFromState.d.ts.map +1 -1
  128. package/lib/typescript/src/getPathFromState.d.ts +3 -3
  129. package/lib/typescript/src/getPathFromState.d.ts.map +1 -1
  130. package/lib/typescript/src/getPatternParts.d.ts +1 -0
  131. package/lib/typescript/src/getPatternParts.d.ts.map +1 -1
  132. package/lib/typescript/src/getStateFromPath.d.ts +5 -5
  133. package/lib/typescript/src/getStateFromPath.d.ts.map +1 -1
  134. package/lib/typescript/src/getStateFromRouteParams.d.ts +3 -0
  135. package/lib/typescript/src/getStateFromRouteParams.d.ts.map +1 -0
  136. package/lib/typescript/src/index.d.ts +34 -33
  137. package/lib/typescript/src/index.d.ts.map +1 -1
  138. package/lib/typescript/src/theming/ThemeContext.d.ts +1 -1
  139. package/lib/typescript/src/theming/ThemeContext.d.ts.map +1 -1
  140. package/lib/typescript/src/theming/ThemeProvider.d.ts +2 -2
  141. package/lib/typescript/src/theming/ThemeProvider.d.ts.map +1 -1
  142. package/lib/typescript/src/theming/useTheme.d.ts +1 -1
  143. package/lib/typescript/src/theming/useTheme.d.ts.map +1 -1
  144. package/lib/typescript/src/types.d.ts +215 -131
  145. package/lib/typescript/src/types.d.ts.map +1 -1
  146. package/lib/typescript/src/useChildListeners.d.ts +3 -3
  147. package/lib/typescript/src/useChildListeners.d.ts.map +1 -1
  148. package/lib/typescript/src/useComponent.d.ts +1 -1
  149. package/lib/typescript/src/useComponent.d.ts.map +1 -1
  150. package/lib/typescript/src/useDescriptors.d.ts +7 -7
  151. package/lib/typescript/src/useDescriptors.d.ts.map +1 -1
  152. package/lib/typescript/src/useEventEmitter.d.ts +1 -1
  153. package/lib/typescript/src/useEventEmitter.d.ts.map +1 -1
  154. package/lib/typescript/src/useFocusEvents.d.ts +2 -2
  155. package/lib/typescript/src/useFocusEvents.d.ts.map +1 -1
  156. package/lib/typescript/src/useFocusedListenersChildrenAdapter.d.ts +2 -2
  157. package/lib/typescript/src/useFocusedListenersChildrenAdapter.d.ts.map +1 -1
  158. package/lib/typescript/src/useIsFocused.d.ts +3 -0
  159. package/lib/typescript/src/useIsFocused.d.ts.map +1 -1
  160. package/lib/typescript/src/useKeyedChildListeners.d.ts +3 -3
  161. package/lib/typescript/src/useKeyedChildListeners.d.ts.map +1 -1
  162. package/lib/typescript/src/useNavigation.d.ts +5 -3
  163. package/lib/typescript/src/useNavigation.d.ts.map +1 -1
  164. package/lib/typescript/src/useNavigationBuilder.d.ts +24 -24
  165. package/lib/typescript/src/useNavigationBuilder.d.ts.map +1 -1
  166. package/lib/typescript/src/useNavigationCache.d.ts +3 -3
  167. package/lib/typescript/src/useNavigationCache.d.ts.map +1 -1
  168. package/lib/typescript/src/useNavigationContainerRef.d.ts +1 -1
  169. package/lib/typescript/src/useNavigationContainerRef.d.ts.map +1 -1
  170. package/lib/typescript/src/useNavigationHelpers.d.ts +19 -19
  171. package/lib/typescript/src/useNavigationHelpers.d.ts.map +1 -1
  172. package/lib/typescript/src/useNavigationState.d.ts +2 -2
  173. package/lib/typescript/src/useNavigationState.d.ts.map +1 -1
  174. package/lib/typescript/src/useOnAction.d.ts +9 -9
  175. package/lib/typescript/src/useOnAction.d.ts.map +1 -1
  176. package/lib/typescript/src/useOnGetState.d.ts +1 -1
  177. package/lib/typescript/src/useOnGetState.d.ts.map +1 -1
  178. package/lib/typescript/src/useOnPreventRemove.d.ts +3 -3
  179. package/lib/typescript/src/useOnPreventRemove.d.ts.map +1 -1
  180. package/lib/typescript/src/useOnRouteFocus.d.ts +6 -6
  181. package/lib/typescript/src/useOnRouteFocus.d.ts.map +1 -1
  182. package/lib/typescript/src/useOptionsGetters.d.ts +1 -1
  183. package/lib/typescript/src/useOptionsGetters.d.ts.map +1 -1
  184. package/lib/typescript/src/usePreventRemoveContext.d.ts +1 -1
  185. package/lib/typescript/src/useRoute.d.ts +2 -2
  186. package/lib/typescript/src/useRoute.d.ts.map +1 -1
  187. package/lib/typescript/src/useRouteCache.d.ts +4 -4
  188. package/lib/typescript/src/useRouteCache.d.ts.map +1 -1
  189. package/lib/typescript/src/useScheduleUpdate.d.ts.map +1 -1
  190. package/lib/typescript/src/useStateForPath.d.ts +1 -1
  191. package/lib/typescript/src/useStateForPath.d.ts.map +1 -1
  192. package/lib/typescript/src/utilities.d.ts +45 -5
  193. package/lib/typescript/src/utilities.d.ts.map +1 -1
  194. package/lib/typescript/src/validatePathConfig.d.ts.map +1 -1
  195. package/package.json +23 -20
  196. package/src/BaseNavigationContainer.tsx +337 -326
  197. package/src/ConsumedParamsContext.tsx +10 -0
  198. package/src/NavigationBuilderContext.tsx +14 -8
  199. package/src/NavigationFocusedRouteStateContext.tsx +4 -4
  200. package/src/NavigationIndependentTree.tsx +8 -5
  201. package/src/NavigationProvider.tsx +17 -3
  202. package/src/NavigationStateContext.tsx +5 -6
  203. package/src/PreventRemoveProvider.tsx +3 -3
  204. package/src/SceneView.tsx +58 -56
  205. package/src/StaticNavigation.tsx +488 -250
  206. package/src/checkSerializable.tsx +22 -12
  207. package/src/createNavigatorFactory.tsx +83 -8
  208. package/src/findFocusedRoute.tsx +13 -8
  209. package/src/getActionFromState.tsx +68 -39
  210. package/src/getPathFromState.tsx +91 -27
  211. package/src/getPatternParts.tsx +8 -0
  212. package/src/getStateFromPath.tsx +555 -180
  213. package/src/getStateFromRouteParams.tsx +60 -0
  214. package/src/index.tsx +47 -5
  215. package/src/theming/useTheme.tsx +1 -1
  216. package/src/types.tsx +541 -338
  217. package/src/useDescriptors.tsx +27 -12
  218. package/src/useEventEmitter.tsx +45 -36
  219. package/src/useFocusEvents.tsx +8 -2
  220. package/src/useFocusedListenersChildrenAdapter.tsx +2 -2
  221. package/src/useIsFocused.tsx +14 -21
  222. package/src/useNavigation.tsx +8 -4
  223. package/src/useNavigationBuilder.tsx +220 -132
  224. package/src/useNavigationCache.tsx +6 -6
  225. package/src/useNavigationHelpers.tsx +1 -1
  226. package/src/useNavigationIndependentTree.tsx +1 -1
  227. package/src/useNavigationState.tsx +11 -7
  228. package/src/useOnAction.tsx +10 -10
  229. package/src/useOnGetState.tsx +5 -3
  230. package/src/useOnPreventRemove.tsx +3 -3
  231. package/src/useOnRouteFocus.tsx +10 -12
  232. package/src/useOptionsGetters.tsx +7 -4
  233. package/src/usePreventRemove.tsx +1 -1
  234. package/src/usePreventRemoveContext.tsx +1 -1
  235. package/src/useRegisterNavigator.tsx +2 -2
  236. package/src/useRoute.tsx +4 -2
  237. package/src/useRouteCache.tsx +2 -1
  238. package/src/useScheduleUpdate.tsx +1 -3
  239. package/src/useStateForPath.tsx +1 -1
  240. package/src/utilities.tsx +101 -5
  241. package/src/validatePathConfig.tsx +1 -0
  242. package/lib/module/CurrentRenderContext.js +0 -10
  243. package/lib/module/CurrentRenderContext.js.map +0 -1
  244. package/lib/module/useCurrentRender.js +0 -19
  245. package/lib/module/useCurrentRender.js.map +0 -1
  246. package/lib/typescript/src/CurrentRenderContext.d.ts +0 -9
  247. package/lib/typescript/src/CurrentRenderContext.d.ts.map +0 -1
  248. package/lib/typescript/src/useCurrentRender.d.ts +0 -14
  249. package/lib/typescript/src/useCurrentRender.d.ts.map +0 -1
  250. package/src/CurrentRenderContext.tsx +0 -9
  251. package/src/useCurrentRender.tsx +0 -31
package/src/types.tsx CHANGED
@@ -14,6 +14,7 @@ import type {
14
14
  FlatType,
15
15
  KeyOf,
16
16
  NotUndefined,
17
+ StandardSchemaV1,
17
18
  UnionToIntersection,
18
19
  } from './utilities';
19
20
 
@@ -54,13 +55,8 @@ export interface RootNavigator {}
54
55
  export interface Theme {}
55
56
 
56
57
  export type RootParamList =
57
- RootNavigator extends TypedNavigatorInternal<
58
- infer ParamList,
59
- any,
60
- any,
61
- any,
62
- any,
63
- any
58
+ RootNavigator extends PrivateValueStore<
59
+ [infer ParamList extends {}, any, any, any]
64
60
  >
65
61
  ? ParamList
66
62
  : {};
@@ -82,58 +78,68 @@ export type DefaultNavigatorOptions<
82
78
  * Layout for the navigator.
83
79
  * Useful for wrapping with a component with access to navigator's state and options.
84
80
  */
85
- layout?: (props: {
86
- state: State;
87
- navigation: NavigationHelpers<ParamList>;
88
- descriptors: Record<
89
- string,
90
- Descriptor<
91
- ScreenOptions,
92
- NavigationProp<
93
- ParamList,
94
- keyof ParamList,
95
- State,
96
- ScreenOptions,
97
- EventMap
98
- >,
99
- RouteProp<ParamList>
100
- >
101
- >;
102
- children: React.ReactNode;
103
- }) => React.ReactElement;
81
+ layout?:
82
+ | ((props: {
83
+ state: State;
84
+ navigation: NavigationHelpers<ParamList>;
85
+ descriptors: Record<
86
+ string,
87
+ Descriptor<
88
+ ScreenOptions,
89
+ NavigationProp<
90
+ ParamList,
91
+ keyof ParamList,
92
+ State,
93
+ ScreenOptions,
94
+ EventMap
95
+ >,
96
+ RouteProp<ParamList>
97
+ >
98
+ >;
99
+ children: React.ReactNode;
100
+ }) => React.ReactElement)
101
+ | undefined;
104
102
 
105
103
  /**
106
104
  * Event listeners for all the screens in the navigator.
107
105
  */
108
106
  screenListeners?:
109
- | ScreenListeners<State, EventMap>
110
- | ((props: {
111
- route: RouteProp<ParamList>;
112
- navigation: Navigation;
113
- }) => ScreenListeners<State, EventMap>);
107
+ | (
108
+ | ScreenListeners<State, EventMap>
109
+ | ((props: {
110
+ route: RouteProp<ParamList>;
111
+ navigation: Navigation;
112
+ }) => ScreenListeners<State, EventMap>)
113
+ )
114
+ | undefined;
114
115
 
115
116
  /**
116
117
  * Default options for all screens under this navigator.
117
118
  */
118
119
  screenOptions?:
119
- | ScreenOptions
120
- | ((props: {
121
- route: RouteProp<ParamList>;
122
- navigation: Navigation;
123
- theme: Theme;
124
- }) => ScreenOptions);
120
+ | (
121
+ | ScreenOptions
122
+ | ((props: {
123
+ route: RouteProp<ParamList>;
124
+ navigation: Navigation;
125
+ theme: Theme;
126
+ }) => ScreenOptions)
127
+ )
128
+ | undefined;
125
129
 
126
130
  /**
127
131
  * Layout for all screens under this navigator.
128
132
  */
129
- screenLayout?: (
130
- props: ScreenLayoutArgs<
131
- ParamList,
132
- keyof ParamList,
133
- ScreenOptions,
134
- Navigation
135
- >
136
- ) => React.ReactElement;
133
+ screenLayout?:
134
+ | ((
135
+ props: ScreenLayoutArgs<
136
+ ParamList,
137
+ keyof ParamList,
138
+ ScreenOptions,
139
+ Navigation
140
+ >
141
+ ) => React.ReactElement)
142
+ | undefined;
137
143
 
138
144
  /**
139
145
  * A function returning overrides for the underlying router used by the navigator.
@@ -142,9 +148,11 @@ export type DefaultNavigatorOptions<
142
148
  *
143
149
  * This must be a pure function and cannot reference outside dynamic variables.
144
150
  */
145
- router?: <Action extends NavigationAction>(
146
- original: Router<State, Action>
147
- ) => Partial<Router<State, Action>>;
151
+ router?:
152
+ | (<Action extends NavigationAction>(
153
+ original: Router<State, Action>
154
+ ) => Partial<Router<State, Action>>)
155
+ | undefined;
148
156
 
149
157
  /**
150
158
  * What should happen when the available route names change.
@@ -165,15 +173,15 @@ export type DefaultNavigatorOptions<
165
173
  * - Direct navigation is only handled for `NAVIGATE` actions.
166
174
  * - Unhandled state is restored only if the current state becomes invalid, i.e. it doesn't contain any currently defined screens.
167
175
  */
168
- routeNamesChangeBehavior?: 'firstMatch' | 'lastUnhandled';
176
+ routeNamesChangeBehavior?: ('firstMatch' | 'lastUnhandled') | undefined;
169
177
  };
170
178
 
171
179
  export type EventMapBase = Record<
172
180
  string,
173
- { data?: any; canPreventDefault?: boolean }
181
+ { data?: any; canPreventDefault?: boolean | undefined }
174
182
  >;
175
183
 
176
- export type EventMapCore<State extends NavigationState> = {
184
+ export type EventMapCore<in out State extends NavigationState> = {
177
185
  focus: { data: undefined };
178
186
  blur: { data: undefined };
179
187
  state: { data: { state: State } };
@@ -189,7 +197,10 @@ export type EventArg<
189
197
  * Type of the event (e.g. `focus`, `blur`)
190
198
  */
191
199
  readonly type: EventName;
192
- readonly target?: string;
200
+ /**
201
+ * Key of the route which received the event.
202
+ */
203
+ readonly target?: string | undefined;
193
204
  } & (CanPreventDefault extends true
194
205
  ? {
195
206
  /**
@@ -203,15 +214,14 @@ export type EventArg<
203
214
  }
204
215
  : {}) &
205
216
  (undefined extends Data
206
- ? { readonly data?: Readonly<Data> }
217
+ ? { readonly data?: Readonly<Data> | undefined }
207
218
  : { readonly data: Readonly<Data> });
208
219
 
209
220
  export type EventListenerCallback<
210
- EventMap extends EventMapBase,
211
- EventName extends keyof EventMap,
212
- EventCanPreventDefault extends
213
- | boolean
214
- | undefined = EventMap[EventName]['canPreventDefault'],
221
+ in out EventMap extends EventMapBase,
222
+ in out EventName extends keyof EventMap,
223
+ in out EventCanPreventDefault extends boolean | undefined =
224
+ EventMap[EventName]['canPreventDefault'],
215
225
  > = (
216
226
  e: EventArg<
217
227
  EventName,
@@ -220,7 +230,7 @@ export type EventListenerCallback<
220
230
  >
221
231
  ) => void;
222
232
 
223
- export type EventConsumer<EventMap extends EventMapBase> = {
233
+ export type EventConsumer<in out EventMap extends EventMapBase> = {
224
234
  /**
225
235
  * Subscribe to events from the parent navigator.
226
236
  *
@@ -237,7 +247,7 @@ export type EventConsumer<EventMap extends EventMapBase> = {
237
247
  ): void;
238
248
  };
239
249
 
240
- export type EventEmitter<EventMap extends EventMapBase> = {
250
+ export type EventEmitter<in out EventMap extends EventMapBase> = {
241
251
  /**
242
252
  * Emit an event to child screens.
243
253
  *
@@ -263,7 +273,7 @@ export type EventEmitter<EventMap extends EventMapBase> = {
263
273
  >;
264
274
  };
265
275
 
266
- export class PrivateValueStore<T extends [any, any, any]> {
276
+ export class PrivateValueStore<T extends [any, any, any, any]> {
267
277
  /**
268
278
  * UGLY HACK! DO NOT USE THE TYPE!!!
269
279
  *
@@ -311,12 +321,18 @@ type NavigationHelpersCommon<
311
321
  ? [
312
322
  screen: RouteName,
313
323
  params?: ParamList[RouteName],
314
- options?: { merge?: boolean; pop?: boolean },
324
+ options?: {
325
+ merge?: boolean | undefined;
326
+ pop?: boolean | undefined;
327
+ },
315
328
  ]
316
329
  : [
317
330
  screen: RouteName,
318
331
  params: ParamList[RouteName],
319
- options?: { merge?: boolean; pop?: boolean },
332
+ options?: {
333
+ merge?: boolean | undefined;
334
+ pop?: boolean | undefined;
335
+ },
320
336
  ]
321
337
  : never
322
338
  ): void;
@@ -335,9 +351,9 @@ type NavigationHelpersCommon<
335
351
  ? {
336
352
  name: RouteName;
337
353
  params: ParamList[RouteName];
338
- path?: string;
339
- merge?: boolean;
340
- pop?: boolean;
354
+ path?: string | undefined;
355
+ merge?: boolean | undefined;
356
+ pop?: boolean | undefined;
341
357
  }
342
358
  : never
343
359
  ): void;
@@ -392,7 +408,7 @@ type NavigationHelpersCommon<
392
408
  * So don't use it in `render`.
393
409
  */
394
410
  getState(): State;
395
- } & PrivateValueStore<[ParamList, unknown, unknown]>;
411
+ };
396
412
 
397
413
  type ParamType<
398
414
  ParamList extends {},
@@ -407,8 +423,8 @@ type ParamType<
407
423
  : unknown;
408
424
 
409
425
  type NavigationHelpersRoute<
410
- ParamList extends {},
411
- RouteName extends keyof ParamList = KeyOf<ParamList>,
426
+ in out ParamList extends {},
427
+ in out RouteName extends keyof ParamList = KeyOf<ParamList>,
412
428
  > = {
413
429
  /**
414
430
  * Update the param object for the route.
@@ -440,43 +456,51 @@ export type NavigationHelpers<
440
456
  EventMap extends EventMapBase = {},
441
457
  > = NavigationHelpersCommon<ParamList> &
442
458
  EventEmitter<EventMap> &
443
- NavigationHelpersRoute<ParamList, keyof ParamList>;
459
+ NavigationHelpersRoute<ParamList, keyof ParamList> &
460
+ PrivateValueStore<[ParamList, unknown, unknown, unknown]>;
444
461
 
445
462
  export type NavigationContainerProps = {
446
463
  /**
447
464
  * Initial state object for the navigation tree.
448
465
  */
449
- initialState?: InitialState;
466
+ initialState?: InitialState | undefined;
450
467
  /**
451
468
  * Callback which is called with the latest navigation state when it changes.
452
469
  */
453
- onStateChange?: (state: Readonly<NavigationState> | undefined) => void;
470
+ onStateChange?:
471
+ | ((state: Readonly<NavigationState> | undefined) => void)
472
+ | undefined;
454
473
  /**
455
474
  * Callback which is called after the navigation tree mounts.
456
475
  */
457
- onReady?: () => void;
476
+ onReady?: (() => void) | undefined;
458
477
  /**
459
478
  * Callback which is called when an action is not handled.
460
479
  */
461
- onUnhandledAction?: (action: Readonly<NavigationAction>) => void;
480
+ onUnhandledAction?:
481
+ | ((action: Readonly<NavigationAction>) => void)
482
+ | undefined;
462
483
  /**
463
484
  * Theme object for the UI elements.
464
485
  */
465
- theme?: Theme;
486
+ theme?: Theme | undefined;
466
487
  /**
467
488
  * Children elements to render.
468
489
  */
469
490
  children: React.ReactNode;
470
491
  };
471
492
 
472
- export type NavigationProp<
493
+ // Everything in `NavigationProp` except `getParent` and the private brand.
494
+ // The composite navigation prop intersects this directly, so it doesn't have
495
+ // to walk `NavigationProp`'s members with `Omit` to drop `getParent`/the brand.
496
+ type NavigationPropBase<
473
497
  ParamList extends {},
474
498
  RouteName extends keyof ParamList = KeyOf<ParamList>,
475
499
  State extends NavigationState = NavigationState<ParamList>,
476
500
  ScreenOptions extends {} = {},
477
501
  EventMap extends EventMapBase = {},
478
502
  ActionHelpers extends Record<string, (...args: any) => void> = {},
479
- > = Omit<NavigationHelpersCommon<ParamList, State>, 'getParent'> & {
503
+ > = NavigationHelpersCommon<ParamList, State> & {
480
504
  /**
481
505
  * Update the options for the route.
482
506
  * The options object will be shallow merged with default options object.
@@ -484,6 +508,25 @@ export type NavigationProp<
484
508
  * @param options Partial options object for the current screen.
485
509
  */
486
510
  setOptions(options: Partial<ScreenOptions>): void;
511
+ } & NavigationHelpersRoute<ParamList, RouteName> &
512
+ ActionHelpers &
513
+ EventConsumer<EventMap & EventMapCore<State>>;
514
+
515
+ export type NavigationProp<
516
+ ParamList extends {},
517
+ RouteName extends keyof ParamList = KeyOf<ParamList>,
518
+ State extends NavigationState = NavigationState<ParamList>,
519
+ ScreenOptions extends {} = {},
520
+ EventMap extends EventMapBase = {},
521
+ ActionHelpers extends Record<string, (...args: any) => void> = {},
522
+ > = NavigationPropBase<
523
+ ParamList,
524
+ RouteName,
525
+ State,
526
+ ScreenOptions,
527
+ EventMap,
528
+ ActionHelpers
529
+ > & {
487
530
  /**
488
531
  * Returns the navigation prop of the parent screen.
489
532
  * If a route name is provided, the navigation prop from the parent screen with matching route name (including current) will be returned.
@@ -502,58 +545,93 @@ export type NavigationProp<
502
545
  ActionHelpers
503
546
  >;
504
547
  getParent(): NavigationProp<ParamListBase> | undefined;
505
- } & NavigationHelpersRoute<ParamList, RouteName> &
506
- ActionHelpers &
507
- EventConsumer<EventMap & EventMapCore<State>> &
508
- PrivateValueStore<[ParamList, RouteName, EventMap]>;
548
+ } & PrivateValueStore<[ParamList, RouteName, EventMap, ActionHelpers]>;
509
549
 
510
550
  export type RouteProp<
511
- ParamList extends ParamListBase,
512
- RouteName extends keyof ParamList = KeyOf<ParamList>,
551
+ in out ParamList extends ParamListBase,
552
+ in out RouteName extends keyof ParamList = KeyOf<ParamList>,
513
553
  > = Route<Extract<RouteName, string>, ParamList[RouteName]>;
514
554
 
515
555
  export type CompositeNavigationProp<
516
556
  A extends NavigationProp<ParamListBase, any, any, any, any>,
517
557
  B extends NavigationProp<ParamListBase, any, any, any, any>,
518
- > = Omit<A & B, keyof NavigationProp<any, any, any, any, any>> &
519
- Omit<
520
- NavigationProp<
521
- /**
522
- * Param list from both navigation objects needs to be combined
523
- * For example, we should be able to navigate to screens in both A and B
524
- */
525
- (A extends NavigationHelpersCommon<infer T> ? T : never) &
526
- (B extends NavigationHelpersCommon<infer U> ? U : never),
527
- /**
528
- * The route name should refer to the route name specified in the first type
529
- * Ideally it should work for any of them, but it's not possible to infer that way
530
- */
531
- A extends NavigationProp<any, infer R> ? R : string,
532
- /**
533
- * The type of state should refer to the state specified in the first type
534
- */
535
- A extends NavigationProp<any, any, infer S> ? S : NavigationState,
536
- /**
537
- * Screen options should refer to the options specified in the first type
538
- */
539
- A extends NavigationProp<any, any, any, infer O> ? O : {},
540
- /**
541
- * Event consumer config should refer to the config specified in the first type
542
- * This allows typechecking `addListener`/`removeListener`
543
- */
544
- A extends NavigationProp<any, any, any, any, infer E> ? E : {}
545
- >,
546
- 'getParent'
547
- > & {
548
- getParent: A['getParent'] & B['getParent'];
549
- };
558
+ > = CompositeNavigationPropInternal<
559
+ A,
560
+ B,
561
+ /**
562
+ * Param list from both navigation objects needs to be combined
563
+ * For example, we should be able to navigate to screens in both A and B
564
+ */
565
+ ParamListOfNavigationProp<A> & ParamListOfNavigationProp<B>,
566
+ /**
567
+ * The route name should refer to the route name specified in the first type
568
+ */
569
+ RouteNameOfNavigationProp<A>,
570
+ /**
571
+ * Event map should refer to the config specified in the first type
572
+ */
573
+ EventMapOfNavigationProp<A>
574
+ >;
575
+
576
+ type CompositeNavigationPropInternal<
577
+ A extends NavigationProp<ParamListBase, any, any, any, any>,
578
+ B extends NavigationProp<ParamListBase, any, any, any, any>,
579
+ ParamList extends {},
580
+ RouteName extends keyof ParamList,
581
+ EventMap extends EventMapBase,
582
+ > = // Action helpers are read from the private brand (4th slot) instead of
583
+ // `Omit<A & B, keyof NavigationProp>`, which had to walk the whole (possibly
584
+ // deeply composed) member set of `A & B` to drop the standard members.
585
+ ActionHelpersOfNavigationProp<A> &
586
+ ActionHelpersOfNavigationProp<B> &
587
+ // Intersect the brand-free base directly instead of `Omit`-ing `getParent`
588
+ // off a full `NavigationProp`, so we skip a mapped-type pass per composite.
589
+ NavigationPropBase<
590
+ ParamList,
591
+ RouteName,
592
+ StateOfNavigationProp<A>,
593
+ ScreenOptionsOfNavigationProp<A>,
594
+ EventMap
595
+ > & {
596
+ getParent: A['getParent'] & B['getParent'];
597
+ } & PrivateValueStore<
598
+ [
599
+ ParamList,
600
+ RouteName,
601
+ EventMap,
602
+ ActionHelpersOfNavigationProp<A> & ActionHelpersOfNavigationProp<B>,
603
+ ]
604
+ >;
605
+
606
+ type PrivateValueOfNavigationProp<T> =
607
+ T extends PrivateValueStore<infer Value> ? Value : [never, unknown, {}, {}];
608
+
609
+ type ParamListOfNavigationProp<T> = PrivateValueOfNavigationProp<T>[0];
610
+
611
+ type RouteNameOfNavigationProp<T> = PrivateValueOfNavigationProp<T>[1];
612
+
613
+ type ActionHelpersOfNavigationProp<T> = PrivateValueOfNavigationProp<T>[3];
614
+
615
+ type StateOfNavigationProp<T> = T extends {
616
+ getState: () => infer State extends NavigationState;
617
+ }
618
+ ? State
619
+ : NavigationState;
620
+
621
+ type ScreenOptionsOfNavigationProp<T> = T extends {
622
+ setOptions: (options: Partial<infer ScreenOptions>) => void;
623
+ }
624
+ ? ScreenOptions
625
+ : {};
626
+
627
+ type EventMapOfNavigationProp<T> = PrivateValueOfNavigationProp<T>[2];
550
628
 
551
629
  export type CompositeScreenProps<
552
- A extends {
630
+ in out A extends {
553
631
  navigation: NavigationProp<ParamListBase, string, any, any, any>;
554
632
  route: RouteProp<ParamListBase>;
555
633
  },
556
- B extends {
634
+ in out B extends {
557
635
  navigation: NavigationProp<ParamListBase, string, any, any, any>;
558
636
  },
559
637
  > = {
@@ -562,10 +640,10 @@ export type CompositeScreenProps<
562
640
  };
563
641
 
564
642
  export type ScreenLayoutArgs<
565
- ParamList extends ParamListBase,
566
- RouteName extends keyof ParamList,
567
- ScreenOptions extends {},
568
- Navigation,
643
+ in out ParamList extends ParamListBase,
644
+ in out RouteName extends keyof ParamList,
645
+ in out ScreenOptions extends {},
646
+ in out Navigation,
569
647
  > = {
570
648
  route: RouteProp<ParamList, RouteName>;
571
649
  options: ScreenOptions;
@@ -575,9 +653,9 @@ export type ScreenLayoutArgs<
575
653
  };
576
654
 
577
655
  export type Descriptor<
578
- ScreenOptions extends {},
579
- Navigation extends NavigationProp<any, any, any, any, any>,
580
- Route extends RouteProp<any, any>,
656
+ out ScreenOptions extends {},
657
+ out Navigation extends NavigationProp<ParamListBase, any, any, any, any>,
658
+ out Route extends RouteProp<any, any>,
581
659
  > = {
582
660
  /**
583
661
  * Render the component associated with this route.
@@ -601,8 +679,8 @@ export type Descriptor<
601
679
  };
602
680
 
603
681
  export type ScreenListeners<
604
- State extends NavigationState,
605
- EventMap extends EventMapBase,
682
+ in out State extends NavigationState,
683
+ in out EventMap extends EventMapBase,
606
684
  > = Partial<{
607
685
  [EventName in keyof (EventMap & EventMapCore<State>)]: EventListenerCallback<
608
686
  EventMap & EventMapCore<State>,
@@ -629,16 +707,16 @@ export type RouteConfigComponent<
629
707
  * React component to render for this screen.
630
708
  */
631
709
  component: ScreenComponentType<ParamList, RouteName>;
632
- getComponent?: never;
633
- children?: never;
710
+ getComponent?: never | undefined;
711
+ children?: never | undefined;
634
712
  }
635
713
  | {
636
714
  /**
637
715
  * Lazily get a React component to render for this screen.
638
716
  */
639
717
  getComponent: () => ScreenComponentType<ParamList, RouteName>;
640
- component?: never;
641
- children?: never;
718
+ component?: never | undefined;
719
+ children?: never | undefined;
642
720
  }
643
721
  | {
644
722
  /**
@@ -648,24 +726,24 @@ export type RouteConfigComponent<
648
726
  route: RouteProp<ParamList, RouteName>;
649
727
  navigation: any;
650
728
  }) => React.ReactNode;
651
- component?: never;
652
- getComponent?: never;
729
+ component?: never | undefined;
730
+ getComponent?: never | undefined;
653
731
  };
654
732
 
655
733
  export type RouteConfigProps<
656
- ParamList extends ParamListBase,
657
- RouteName extends keyof ParamList,
658
- State extends NavigationState,
659
- ScreenOptions extends {},
660
- EventMap extends EventMapBase,
661
- Navigation,
734
+ in out ParamList extends ParamListBase,
735
+ in out RouteName extends keyof ParamList,
736
+ in out State extends NavigationState,
737
+ in out ScreenOptions extends {},
738
+ in out EventMap extends EventMapBase,
739
+ in out Navigation,
662
740
  > = {
663
741
  /**
664
742
  * Optional key for this screen. This doesn't need to be unique.
665
743
  * If the key changes, existing screens with this name will be removed or reset.
666
744
  * Useful when we have some common screens and have conditional rendering.
667
745
  */
668
- navigationKey?: string;
746
+ navigationKey?: string | undefined;
669
747
 
670
748
  /**
671
749
  * Route name of this screen.
@@ -676,31 +754,39 @@ export type RouteConfigProps<
676
754
  * Navigator options for this screen.
677
755
  */
678
756
  options?:
679
- | ScreenOptions
680
- | ((props: {
681
- route: RouteProp<ParamList, RouteName>;
682
- navigation: Navigation;
683
- theme: Theme;
684
- }) => ScreenOptions);
757
+ | (
758
+ | ScreenOptions
759
+ | ((props: {
760
+ route: RouteProp<ParamList, RouteName>;
761
+ navigation: Navigation;
762
+ theme: Theme;
763
+ }) => ScreenOptions)
764
+ )
765
+ | undefined;
685
766
 
686
767
  /**
687
768
  * Event listeners for this screen.
688
769
  */
689
770
  listeners?:
690
- | ScreenListeners<State, EventMap>
691
- | ((props: {
692
- route: RouteProp<ParamList, RouteName>;
693
- navigation: Navigation;
694
- }) => ScreenListeners<State, EventMap>);
771
+ | (
772
+ | ScreenListeners<State, EventMap>
773
+ | ((props: {
774
+ route: RouteProp<ParamList, RouteName>;
775
+ navigation: Navigation;
776
+ }) => ScreenListeners<State, EventMap>)
777
+ )
778
+ | undefined;
695
779
 
696
780
  /**
697
781
  * Layout for this screen.
698
782
  * Useful for wrapping the screen with custom containers.
699
783
  * e.g. for styling, error boundaries, suspense, etc.
700
784
  */
701
- layout?: (
702
- props: ScreenLayoutArgs<ParamList, RouteName, ScreenOptions, Navigation>
703
- ) => React.ReactElement;
785
+ layout?:
786
+ | ((
787
+ props: ScreenLayoutArgs<ParamList, RouteName, ScreenOptions, Navigation>
788
+ ) => React.ReactElement)
789
+ | undefined;
704
790
 
705
791
  /**
706
792
  * Function to return an unique ID for this screen.
@@ -708,16 +794,18 @@ export type RouteConfigProps<
708
794
  * For a given screen name, there will always be only one screen corresponding to an ID.
709
795
  * If `undefined` is returned, it acts same as no `getId` being specified.
710
796
  */
711
- getId?: ({
712
- params,
713
- }: {
714
- params: Readonly<ParamList[RouteName]>;
715
- }) => string | undefined;
797
+ getId?:
798
+ | (({
799
+ params,
800
+ }: {
801
+ params: Readonly<ParamList[RouteName]>;
802
+ }) => string | undefined)
803
+ | undefined;
716
804
 
717
805
  /**
718
806
  * Initial params object for the route.
719
807
  */
720
- initialParams?: Partial<ParamList[RouteName]>;
808
+ initialParams?: Partial<ParamList[RouteName]> | undefined;
721
809
  };
722
810
 
723
811
  export type RouteConfig<
@@ -738,43 +826,49 @@ export type RouteConfig<
738
826
  RouteConfigComponent<ParamList, RouteName>;
739
827
 
740
828
  export type RouteGroupConfig<
741
- ParamList extends ParamListBase,
742
- ScreenOptions extends {},
743
- Navigation,
829
+ in out ParamList extends ParamListBase,
830
+ in out ScreenOptions extends {},
831
+ in out Navigation,
744
832
  > = {
745
833
  /**
746
834
  * Optional key for the screens in this group.
747
835
  * If the key changes, all existing screens in this group will be removed or reset.
748
836
  */
749
- navigationKey?: string;
837
+ navigationKey?: string | undefined;
750
838
 
751
839
  /**
752
840
  * Navigator options for this screen.
753
841
  */
754
842
  screenOptions?:
755
- | ScreenOptions
756
- | ((props: {
757
- route: RouteProp<ParamList, keyof ParamList>;
758
- navigation: Navigation;
759
- theme: Theme;
760
- }) => ScreenOptions);
843
+ | (
844
+ | ScreenOptions
845
+ | ((props: {
846
+ route: RouteProp<ParamList, keyof ParamList>;
847
+ navigation: Navigation;
848
+ theme: Theme;
849
+ }) => ScreenOptions)
850
+ )
851
+ | undefined;
761
852
 
762
853
  /**
763
854
  * Layout for the screens inside the group.
764
855
  * This will override the `screenLayout` of parent group or navigator.
765
856
  */
766
857
  screenLayout?:
767
- | ((
768
- props: ScreenLayoutArgs<
769
- ParamList,
770
- keyof ParamList,
771
- ScreenOptions,
772
- Navigation
773
- >
774
- ) => React.ReactElement)
775
- | {
776
- // FIXME: TypeScript doesn't seem to infer `navigation` correctly without this
777
- };
858
+ | (
859
+ | ((
860
+ props: ScreenLayoutArgs<
861
+ ParamList,
862
+ keyof ParamList,
863
+ ScreenOptions,
864
+ Navigation
865
+ >
866
+ ) => React.ReactElement)
867
+ | {
868
+ // FIXME: TypeScript doesn't seem to infer `navigation` correctly without this
869
+ }
870
+ )
871
+ | undefined;
778
872
 
779
873
  /**
780
874
  * Children React Elements to extract the route configuration from.
@@ -826,6 +920,31 @@ export type NavigationContainerEventMap = {
826
920
  stack: string | undefined;
827
921
  };
828
922
  };
923
+ /**
924
+ * Event that fires when an event is emitted.
925
+ * Only intended for debugging purposes, don't use it for app logic.
926
+ * This event will be emitted after all listeners have been called.
927
+ */
928
+ __unsafe_event__: {
929
+ data: {
930
+ /**
931
+ * The type of the event that was emitted.
932
+ */
933
+ type: string;
934
+ /**
935
+ * The data object passed when emitting the event.
936
+ */
937
+ data: unknown;
938
+ /**
939
+ * The key of the target route which received the event.
940
+ */
941
+ target: string | undefined;
942
+ /**
943
+ * Whether `event.preventDefault()` was called on this event.
944
+ */
945
+ defaultPrevented: boolean | undefined;
946
+ };
947
+ };
829
948
  };
830
949
 
831
950
  /**
@@ -894,7 +1013,12 @@ export type GenericNavigation<ParamList extends {}> = Omit<
894
1013
  export type RouteForName<
895
1014
  ParamList extends {},
896
1015
  RouteName extends string,
897
- > = Extract<ParamListRoute<ParamList>, { name: RouteName }>;
1016
+ > = RouteForNameInternal<RouteName, ParamListRoute<ParamList>>;
1017
+
1018
+ type RouteForNameInternal<
1019
+ RouteName extends string,
1020
+ Routes,
1021
+ > = string extends RouteName ? Routes : Extract<Routes, { name: RouteName }>;
898
1022
 
899
1023
  type ParamListRoute<ParamList extends {}> = {
900
1024
  [RouteName in keyof ParamList]: NavigatorScreenParams<{}> extends ParamList[RouteName]
@@ -910,11 +1034,10 @@ type MaybeParamListRoute<ParamList extends {}> = ParamList extends ParamListBase
910
1034
 
911
1035
  type BasicNavigationComposite<
912
1036
  Navigation extends NavigationProp<any, any, any, any, any>,
913
- Parent,
914
- > =
915
- Parent extends NavigationProp<any, any, any, any, any>
916
- ? CompositeNavigationProp<Navigation, Parent>
917
- : Navigation;
1037
+ Parent extends NavigationProp<any, any, any, any, any> | undefined,
1038
+ > = Parent extends undefined
1039
+ ? Navigation
1040
+ : CompositeNavigationProp<Navigation, NonNullable<Parent>>;
918
1041
 
919
1042
  type BasicNavigationList<
920
1043
  ParamList extends {},
@@ -926,11 +1049,13 @@ type BasicNavigationList<
926
1049
  ? NotUndefined<ParamList[RouteName]> extends NavigatorScreenParams<
927
1050
  infer T
928
1051
  >
929
- ? BasicNavigationList<
930
- T,
931
- ExcludedRouteNames,
932
- NavigationProp<ParamList, RouteName>
933
- >
1052
+ ? keyof T extends ExcludedRouteNames
1053
+ ? {}
1054
+ : BasicNavigationList<
1055
+ T,
1056
+ ExcludedRouteNames,
1057
+ NavigationProp<ParamList, RouteName>
1058
+ >
934
1059
  : {}
935
1060
  : {}) &
936
1061
  (RouteName extends ExcludedRouteNames
@@ -945,161 +1070,220 @@ type BasicNavigationList<
945
1070
  >;
946
1071
 
947
1072
  export type NavigationListForNavigator<Navigator> =
948
- Navigator extends TypedNavigator<infer Bag, any>
949
- ? Bag['NavigationList']
950
- : Navigator extends PrivateValueStore<[any, infer NavigationList, any]>
951
- ? NavigationList
952
- : {};
953
-
954
- export type NavigationListForNested<Navigator> = FlatType<
955
- NavigationListForNestedInternal<Navigator> &
956
- (Navigator extends TypedNavigator<infer Bag, any>
957
- ? BasicNavigationList<
958
- Bag['ParamList'],
959
- keyof NavigationListForNestedInternal<Navigator>,
960
- undefined
961
- >
962
- : Navigator extends PrivateValueStore<[infer ParamList, any, any]>
963
- ? ParamList extends {}
964
- ? BasicNavigationList<
965
- ParamList,
966
- keyof NavigationListForNestedInternal<Navigator>,
967
- undefined
968
- >
969
- : {}
970
- : {})
971
- >;
972
-
973
- type NavigationListForNestedInternal<Navigator> =
974
- NavigationListForNavigator<Navigator> &
975
- NavigationListForStaticConfig<
976
- NavigationListForNavigator<Navigator>,
977
- Navigator
978
- >;
979
-
980
- type NavigationListWithComposite<
981
- Parent extends NavigationProp<any, any, any, any, any>,
982
- NavigatorList extends Record<string, any>,
983
- > = {
984
- [K in keyof NavigatorList]: CompositeNavigationProp<NavigatorList[K], Parent>;
985
- };
986
-
987
- type NavigationListForStaticConfig<ParentList, Navigator> = Navigator extends {
1073
+ Navigator extends PrivateValueStore<infer Value> ? Value[1] : {};
1074
+
1075
+ export type NavigationListForNested<Navigator> =
1076
+ NavigationListForNestedInternal<Navigator> extends infer NavigationList
1077
+ ? Navigator extends PrivateValueStore<[infer ParamList, any, any, any]>
1078
+ ? ParamList extends {}
1079
+ ? NavigationList &
1080
+ BasicNavigationList<ParamList, keyof NavigationList, undefined>
1081
+ : NavigationList
1082
+ : NavigationList
1083
+ : never;
1084
+
1085
+ type NavigationListForNestedInternal<
1086
+ Navigator,
1087
+ Parent = undefined,
1088
+ NavigatorList extends Record<string, any> =
1089
+ NavigationListForNavigator<Navigator>,
1090
+ // This navigator's own routes, each composed with the accumulated parent.
1091
+ // It's threaded into the recursion as the parent for nested navigators so
1092
+ // each route is composed with its ancestors exactly once, instead of
1093
+ // re-wrapping the whole descendant list at every level (which is quadratic
1094
+ // in the nesting depth).
1095
+ ComposedList = ComposeNavigationList<NavigatorList, Parent>,
1096
+ > = Navigator extends {
988
1097
  readonly config: {
989
- screens?: any;
990
- groups?: any;
1098
+ readonly screens?: infer Screens;
1099
+ readonly groups?: infer Groups;
991
1100
  };
992
1101
  }
993
- ? NavigationListForScreens<ParentList, Navigator['config']['screens']> &
994
- NavigationListForGroups<ParentList, Navigator['config']['groups']>
1102
+ ? ComposedList &
1103
+ NavigationListForScreens<ComposedList, Screens> &
1104
+ NavigationListForGroups<ComposedList, Groups>
1105
+ : ComposedList;
1106
+
1107
+ // Compose every entry in a navigator's list with the accumulated parent.
1108
+ // When there's no parent (root navigator), the list is returned as-is so the
1109
+ // common shallow case doesn't pay for a redundant mapped type.
1110
+ type ComposeNavigationList<NavigatorList extends Record<string, any>, Parent> =
1111
+ Parent extends NavigationProp<any, any, any, any, any>
1112
+ ? {
1113
+ [K in keyof NavigatorList]: CompositeNavigationProp<
1114
+ NavigatorList[K],
1115
+ Parent
1116
+ >;
1117
+ }
1118
+ : NavigatorList;
1119
+
1120
+ type NavigationListForScreens<ParentList, Screens> = Screens extends {}
1121
+ ? UnionToIntersection<
1122
+ {
1123
+ // Only check screens with static config to avoid overly-complex types
1124
+ // Otherwise TypeScript fails to load the types due to complexity
1125
+ [K in keyof Screens]: Screens[K] extends { config: any }
1126
+ ? K extends keyof ParentList
1127
+ ? NavigationListForNestedInternal<Screens[K], ParentList[K]>
1128
+ : never
1129
+ : Screens[K] extends { screen: { config: any } }
1130
+ ? K extends keyof ParentList
1131
+ ? NavigationListForNestedInternal<
1132
+ Screens[K]['screen'],
1133
+ ParentList[K]
1134
+ >
1135
+ : never
1136
+ : never;
1137
+ }[keyof Screens]
1138
+ >
995
1139
  : {};
996
1140
 
997
- type NavigationListForScreens<ParentList, Screens> = UnionToIntersection<
998
- {
999
- // Only check screens with static config to avoid overly-complex types
1000
- // Otherwise TypeScript fails to load the types due to complexity
1001
- [K in keyof Screens]: ParentList extends Record<K, any>
1002
- ? Screens[K] extends { config: any }
1003
- ? NavigationListWithComposite<
1004
- ParentList[K],
1005
- NavigationListForNested<Screens[K]>
1006
- >
1007
- : Screens[K] extends { screen: { config: any } }
1008
- ? NavigationListWithComposite<
1009
- ParentList[K],
1010
- NavigationListForNested<Screens[K]['screen']>
1011
- >
1012
- : {}
1013
- : {};
1014
- }[keyof Screens]
1015
- >;
1016
-
1017
- type NavigationListForGroups<ParentList, Groups> =
1018
- Groups extends Record<string, { screens: any }>
1019
- ? UnionToIntersection<
1020
- {
1021
- [K in keyof Groups]: NavigationListForScreens<
1022
- ParentList,
1023
- Groups[K]['screens']
1024
- >;
1025
- }[keyof Groups]
1026
- >
1027
- : {};
1141
+ type NavigationListForGroups<ParentList, Groups> = Groups extends {}
1142
+ ? UnionToIntersection<
1143
+ {
1144
+ [K in keyof Groups]: Groups[K] extends { screens: any }
1145
+ ? NavigationListForScreens<ParentList, Groups[K]['screens']>
1146
+ : never;
1147
+ }[keyof Groups]
1148
+ >
1149
+ : {};
1028
1150
 
1029
- export type NavigationContainerRef<ParamList extends {}> =
1030
- NavigationHelpers<ParamList> &
1031
- EventConsumer<NavigationContainerEventMap> & {
1032
- /**
1033
- * Reset the navigation state of the root navigator to the provided state.
1034
- *
1035
- * @param state Navigation state object.
1036
- */
1037
- resetRoot(state?: PartialState<NavigationState> | NavigationState): void;
1038
- /**
1039
- * Get the rehydrated navigation state of the navigation tree.
1040
- */
1041
- getRootState(): NavigationState;
1042
- /**
1043
- * Get the currently focused navigation route.
1044
- */
1045
- getCurrentRoute(): MaybeParamListRoute<ParamList> | undefined;
1046
- /**
1047
- * Get the currently focused route's options.
1048
- */
1049
- getCurrentOptions(): object | undefined;
1050
- /**
1051
- * Whether the navigation container is ready to handle actions.
1052
- */
1053
- isReady(): boolean;
1054
- /**
1055
- * Stub function for setOptions on navigation object for use with useNavigation.
1056
- */
1057
- setOptions(): never;
1058
- /**
1059
- * Stub function for getParent on navigation object for use with useNavigation.
1060
- */
1061
- getParent(): undefined;
1062
- };
1151
+ export type NavigationContainerRef<ParamList extends {}> = Omit<
1152
+ NavigationHelpers<ParamList>,
1153
+ keyof NavigationHelpersRoute<{}>
1154
+ > &
1155
+ NavigationHelpersRoute<{}> &
1156
+ EventConsumer<NavigationContainerEventMap> & {
1157
+ /**
1158
+ * Reset the navigation state of the root navigator to the provided state.
1159
+ *
1160
+ * @param state Navigation state object.
1161
+ */
1162
+ resetRoot(state?: PartialState<NavigationState> | NavigationState): void;
1163
+ /**
1164
+ * Get the rehydrated navigation state of the navigation tree.
1165
+ */
1166
+ getRootState(): NavigationState;
1167
+ /**
1168
+ * Get the currently focused navigation route.
1169
+ */
1170
+ getCurrentRoute(): MaybeParamListRoute<ParamList> | undefined;
1171
+ /**
1172
+ * Get the currently focused route's options.
1173
+ */
1174
+ getCurrentOptions(): object | undefined;
1175
+ /**
1176
+ * Whether the navigation container is ready to handle actions.
1177
+ */
1178
+ isReady(): boolean;
1179
+ /**
1180
+ * Stub function for setOptions on navigation object for use with useNavigation.
1181
+ */
1182
+ setOptions(): never;
1183
+ };
1063
1184
 
1064
1185
  export type NavigationContainerRefWithCurrent<ParamList extends {}> =
1065
1186
  NavigationContainerRef<ParamList> & {
1066
1187
  current: NavigationContainerRef<ParamList> | null;
1067
1188
  };
1068
1189
 
1069
- export type NavigationListBase<ParamList extends ParamListBase> = {
1190
+ export type NavigationListBase<in out ParamList extends ParamListBase> = {
1070
1191
  [RouteName in keyof ParamList]: unknown;
1071
1192
  };
1072
1193
 
1073
- export type NavigatorTypeBagBase = {
1194
+ // Only use action helpers if all values are functions
1195
+ type ActionHelpersOf<T> =
1196
+ T extends Record<string, (...args: any) => void> ? T : {};
1197
+
1198
+ export interface NavigatorTypeBagBase {
1074
1199
  ParamList: {};
1075
1200
  State: NavigationState;
1076
1201
  ScreenOptions: {};
1077
1202
  EventMap: {};
1078
- NavigationList: NavigationListBase<ParamListBase>;
1203
+ ActionHelpers: {};
1204
+ NavigationList: {
1205
+ [RouteName in keyof this['ParamList']]: NavigationProp<
1206
+ this['ParamList'],
1207
+ RouteName,
1208
+ this['State'],
1209
+ this['ScreenOptions'],
1210
+ this['EventMap'],
1211
+ ActionHelpersOf<this['ActionHelpers']>
1212
+ >;
1213
+ };
1079
1214
  Navigator: React.ComponentType<any>;
1215
+ }
1216
+
1217
+ /**
1218
+ * Adds a proper `ParamList` to a type bag interface
1219
+ * So it can be used as `this['ParamList']`
1220
+ */
1221
+ export type NavigatorTypeBagFor<
1222
+ TypeBag extends NavigatorTypeBagBase,
1223
+ ParamList extends {},
1224
+ > = TypeBag & { ParamList: ParamList };
1225
+
1226
+ type TypedNavigatorComponent<Bag extends NavigatorTypeBagBase> =
1227
+ React.ComponentType<
1228
+ Omit<
1229
+ React.ComponentProps<
1230
+ TypedNavigatorInternal<
1231
+ Bag['ParamList'],
1232
+ Bag['State'],
1233
+ Bag['ScreenOptions'],
1234
+ Bag['EventMap'],
1235
+ Bag['NavigationList'],
1236
+ Bag['Navigator']
1237
+ >['Navigator']
1238
+ >,
1239
+ 'children'
1240
+ >
1241
+ >;
1242
+
1243
+ type TypedNavigatorStaticDecorated<Bag extends NavigatorTypeBagBase, Config> = {
1244
+ getComponent: () => React.ComponentType<{}>;
1245
+ config: Config;
1246
+ } & PrivateValueStore<
1247
+ [Bag['ParamList'], Bag['NavigationList'], unknown, unknown]
1248
+ >;
1249
+
1250
+ type TypedNavigatorStatic<
1251
+ in out Bag extends NavigatorTypeBagBase,
1252
+ in out Config,
1253
+ > = {
1254
+ config: Config;
1255
+ with: (
1256
+ Component: React.ComponentType<{
1257
+ Navigator: TypedNavigatorComponent<Bag>;
1258
+ }>
1259
+ ) => TypedNavigatorStaticDecorated<Bag, Config>;
1260
+ getComponent: () => TypedNavigatorComponent<Bag>;
1080
1261
  };
1081
1262
 
1082
1263
  export type TypedNavigator<
1083
1264
  Bag extends NavigatorTypeBagBase,
1084
1265
  Config = unknown,
1085
- > = TypedNavigatorInternal<
1086
- Bag['ParamList'],
1087
- Bag['State'],
1088
- Bag['ScreenOptions'],
1089
- Bag['EventMap'],
1090
- Bag['NavigationList'],
1091
- Bag['Navigator']
1092
- > &
1093
- (undefined extends Config ? {} : { config: Config }) &
1094
- PrivateValueStore<[Bag['ParamList'], Bag['NavigationList'], unknown]>;
1266
+ > = (undefined extends Config
1267
+ ? TypedNavigatorInternal<
1268
+ Bag['ParamList'],
1269
+ Bag['State'],
1270
+ Bag['ScreenOptions'],
1271
+ Bag['EventMap'],
1272
+ Bag['NavigationList'],
1273
+ Bag['Navigator']
1274
+ >
1275
+ : TypedNavigatorStatic<Bag, Config>) &
1276
+ PrivateValueStore<
1277
+ [Bag['ParamList'], Bag['NavigationList'], unknown, unknown]
1278
+ >;
1095
1279
 
1096
1280
  type TypedNavigatorInternal<
1097
- ParamList extends ParamListBase,
1098
- State extends NavigationState,
1099
- ScreenOptions extends {},
1100
- EventMap extends EventMapBase,
1101
- NavigationList extends NavigationListBase<ParamList>,
1102
- Navigator extends React.ComponentType<any>,
1281
+ in out ParamList extends ParamListBase,
1282
+ in out State extends NavigationState,
1283
+ in out ScreenOptions extends {},
1284
+ in out EventMap extends EventMapBase,
1285
+ in out NavigationList extends NavigationListBase<ParamList>,
1286
+ in out Navigator extends React.ComponentType<any>,
1103
1287
  > = {
1104
1288
  /**
1105
1289
  * Navigator component which manages the child screens.
@@ -1145,33 +1329,38 @@ export type NavigatorScreenParams<ParamList extends {}> =
1145
1329
  merge?: never;
1146
1330
  initial?: never;
1147
1331
  pop?: never;
1148
- path?: string;
1332
+ preload?: never;
1333
+ path?: string | undefined;
1149
1334
  state: PartialState<NavigationState> | NavigationState | undefined;
1150
1335
  }
1151
1336
  | {
1152
1337
  [RouteName in keyof ParamList]: undefined extends ParamList[RouteName]
1153
1338
  ? {
1154
1339
  screen: RouteName;
1155
- params?: ParamList[RouteName];
1156
- merge?: boolean;
1157
- initial?: boolean;
1158
- path?: string;
1159
- pop?: boolean;
1340
+ params?: ParamList[RouteName] | undefined;
1341
+ merge?: boolean | undefined;
1342
+ initial?: boolean | undefined;
1343
+ path?: string | undefined;
1344
+ pop?: boolean | undefined;
1345
+ preload?: boolean | undefined;
1160
1346
  state?: never;
1161
1347
  }
1162
1348
  : {
1163
1349
  screen: RouteName;
1164
1350
  params: ParamList[RouteName];
1165
- merge?: boolean;
1166
- initial?: boolean;
1167
- path?: string;
1168
- pop?: boolean;
1351
+ merge?: boolean | undefined;
1352
+ initial?: boolean | undefined;
1353
+ path?: string | undefined;
1354
+ pop?: boolean | undefined;
1355
+ preload?: boolean | undefined;
1169
1356
  state?: never;
1170
1357
  };
1171
1358
  }[keyof ParamList];
1172
1359
 
1173
1360
  type ParseConfig<Params> = {
1174
- [K in keyof Params]?: (value: string) => Params[K];
1361
+ [K in keyof Params]?:
1362
+ | ((value: string) => Params[K])
1363
+ | StandardSchemaV1<string | string[] | null | undefined, Params[K]>;
1175
1364
  };
1176
1365
 
1177
1366
  type StringifyConfig<Params> = {
@@ -1189,9 +1378,9 @@ type PathConfigAlias<Params> = {
1189
1378
  * By default, paths are relating to the path config on the parent screen.
1190
1379
  * If `exact` is set to `true`, the parent path configuration is not used.
1191
1380
  */
1192
- exact?: boolean;
1381
+ exact?: boolean | undefined;
1193
1382
  /**
1194
- * An object mapping the param name to a function which parses the param value.
1383
+ * An object mapping the param name to a parser function or a Standard Schema.
1195
1384
  *
1196
1385
  * @example
1197
1386
  * ```js
@@ -1201,7 +1390,18 @@ type PathConfigAlias<Params> = {
1201
1390
  * }
1202
1391
  * ```
1203
1392
  */
1204
- parse?: ParseConfig<Params>;
1393
+ parse?: ParseConfig<Params> | undefined;
1394
+ /**
1395
+ * Whether this path can resolve to multiple routes.
1396
+ * Both routes should specify `shared: true` for this to work.
1397
+ * By default, a path can only resolve to one route.
1398
+ *
1399
+ * This is useful when same screen used in multiple navigators,
1400
+ * e.g. tabs with stacks containing profile screen in each stack.
1401
+ *
1402
+ * The path defined first will be the canonical path.
1403
+ */
1404
+ shared?: boolean | undefined;
1205
1405
  };
1206
1406
 
1207
1407
  export type PathConfig<Params> = FlatType<
@@ -1218,25 +1418,28 @@ export type PathConfig<Params> = FlatType<
1218
1418
  * }
1219
1419
  * ```
1220
1420
  */
1221
- stringify?: StringifyConfig<Params>;
1421
+ stringify?: StringifyConfig<Params> | undefined;
1222
1422
  /**
1223
1423
  * Additional path alias that will be matched to the same screen.
1224
1424
  */
1225
- alias?: (string | PathConfigAlias<Params>)[];
1425
+ alias?: (string | PathConfigAlias<Params>)[] | undefined;
1226
1426
  } & (NonNullable<Params> extends NavigatorScreenParams<infer ParamList>
1227
1427
  ? {
1228
1428
  /**
1229
1429
  * Path configuration for child screens.
1230
1430
  */
1231
- screens?: PathConfigMap<ParamList>;
1431
+ screens?: PathConfigMap<ParamList> | undefined;
1232
1432
  /**
1233
1433
  * Name of the initial route to use for the navigator when the path matches.
1234
1434
  */
1235
- initialRouteName?: keyof ParamList;
1435
+ initialRouteName?: Extract<keyof ParamList, string> | undefined;
1236
1436
  }
1237
1437
  : {})
1238
1438
  >;
1239
1439
 
1240
1440
  export type PathConfigMap<ParamList extends {}> = {
1241
- [RouteName in keyof ParamList]?: string | PathConfig<ParamList[RouteName]>;
1441
+ [RouteName in keyof ParamList]?:
1442
+ | string
1443
+ | PathConfig<ParamList[RouteName]>
1444
+ | undefined;
1242
1445
  };