diff --git a/packages/compat/src/createCompatNavigationProp.tsx b/packages/compat/src/createCompatNavigationProp.tsx index de2ee3c5..505c4ed7 100644 --- a/packages/compat/src/createCompatNavigationProp.tsx +++ b/packages/compat/src/createCompatNavigationProp.tsx @@ -16,10 +16,6 @@ type EventName = | 'didBlur' | 'refocus'; -// const focusSubscriptions = new WeakMap<() => void, () => void>(); -// const blurSubscriptions = new WeakMap<() => void, () => void>(); -// const refocusSubscriptions = new WeakMap<() => void, () => void>(); - export default function createCompatNavigationProp< NavigationPropType extends NavigationProp, ParamList extends ParamListBase = NavigationPropType extends NavigationProp< diff --git a/packages/compat/src/createCompatNavigatorFactory.tsx b/packages/compat/src/createCompatNavigatorFactory.tsx index 86dbc443..d34e8818 100644 --- a/packages/compat/src/createCompatNavigatorFactory.tsx +++ b/packages/compat/src/createCompatNavigatorFactory.tsx @@ -6,7 +6,6 @@ import { TypedNavigator, NavigationProp, RouteProp, - EventMapBase, NavigationRouteContext, } from '@react-navigation/native'; import CompatScreen from './CompatScreen'; @@ -19,7 +18,7 @@ export default function createCompatNavigatorFactory< ParamListBase, NavigationState, {}, - EventMapBase, + any, React.ComponentType > >(createNavigator: CreateNavigator) { @@ -116,7 +115,8 @@ export default function createCompatNavigatorFactory< typeof screenNavigationOptions === 'function' ? { navigation: createCompatNavigationProp< - NavigationPropType + NavigationPropType, + ParamList >(navigation, route, {}), navigationOptions: defaultNavigationOptions || {}, screenProps, diff --git a/packages/core/src/BaseNavigationContainer.tsx b/packages/core/src/BaseNavigationContainer.tsx index 782e86e0..2288ee97 100644 --- a/packages/core/src/BaseNavigationContainer.tsx +++ b/packages/core/src/BaseNavigationContainer.tsx @@ -17,8 +17,11 @@ import useOptionsGetters from './useOptionsGetters'; import useEventEmitter from './useEventEmitter'; import useSyncState from './useSyncState'; import isSerializable from './isSerializable'; - -import type { NavigationContainerRef, NavigationContainerProps } from './types'; +import type { + NavigationContainerEventMap, + NavigationContainerRef, + NavigationContainerProps, +} from './types'; type State = NavigationState | PartialState | undefined; @@ -175,7 +178,7 @@ const BaseNavigationContainer = React.forwardRef( return state.routes[state.index]; }, [getRootState]); - const emitter = useEventEmitter(); + const emitter = useEventEmitter(); const { addOptionsGetter, getCurrentOptions } = useOptionsGetters({}); diff --git a/packages/core/src/types.tsx b/packages/core/src/types.tsx index 77330455..5e41864b 100644 --- a/packages/core/src/types.tsx +++ b/packages/core/src/types.tsx @@ -61,7 +61,7 @@ export type EventArg< preventDefault(): void; } : {}) & - (Data extends undefined ? {} : { readonly data: Data }); + (undefined extends Data ? {} : { readonly data: Data }); export type EventListenerCallback< EventMap extends EventMapBase, @@ -107,7 +107,7 @@ export type EventEmitter = { } & (EventMap[EventName]['canPreventDefault'] extends true ? { canPreventDefault: true } : {}) & - (EventMap[EventName]['data'] extends undefined + (undefined extends EventMap[EventName]['data'] ? {} : { data: EventMap[EventName]['data'] }) ): EventArg< @@ -275,7 +275,7 @@ export type RouteProp< ParamList extends ParamListBase, RouteName extends keyof ParamList > = Omit>, 'params'> & - (ParamList[RouteName] extends undefined + (undefined extends ParamList[RouteName] ? {} : { /** @@ -419,7 +419,7 @@ export type NavigationContainerEventMap = { /** * The updated state object after the state change. */ - state: NavigationState; + state: NavigationState | PartialState | undefined; }; }; /** diff --git a/packages/core/src/useDescriptors.tsx b/packages/core/src/useDescriptors.tsx index 4f8b498a..62a6c770 100644 --- a/packages/core/src/useDescriptors.tsx +++ b/packages/core/src/useDescriptors.tsx @@ -51,7 +51,7 @@ type Options< addStateGetter: (key: string, getter: NavigatorStateGetter) => void; onRouteFocus: (key: string) => void; router: Router; - emitter: NavigationEventEmitter; + emitter: NavigationEventEmitter; }; /** diff --git a/packages/core/src/useEventEmitter.tsx b/packages/core/src/useEventEmitter.tsx index 6109acec..0805703c 100644 --- a/packages/core/src/useEventEmitter.tsx +++ b/packages/core/src/useEventEmitter.tsx @@ -1,8 +1,10 @@ import * as React from 'react'; import type { EventEmitter, EventConsumer, EventArg } from './types'; -export type NavigationEventEmitter = EventEmitter> & { - create: (target: string) => EventConsumer>; +export type NavigationEventEmitter< + T extends Record +> = EventEmitter & { + create: (target: string) => EventConsumer; }; type Listeners = ((e: any) => void)[]; @@ -10,9 +12,9 @@ type Listeners = ((e: any) => void)[]; /** * Hook to manage the event system used by the navigator to notify screens of various events. */ -export default function useEventEmitter( +export default function useEventEmitter>( listen?: (e: any) => void -): NavigationEventEmitter { +): NavigationEventEmitter { const listenRef = React.useRef(listen); React.useEffect(() => { diff --git a/packages/core/src/useFocusEvents.tsx b/packages/core/src/useFocusEvents.tsx index b74bd299..91f9a1fb 100644 --- a/packages/core/src/useFocusEvents.tsx +++ b/packages/core/src/useFocusEvents.tsx @@ -2,16 +2,20 @@ import * as React from 'react'; import type { NavigationState } from '@react-navigation/routers'; import NavigationContext from './NavigationContext'; import type { NavigationEventEmitter } from './useEventEmitter'; +import type { EventMapCore } from './types'; -type Options = { - state: NavigationState; - emitter: NavigationEventEmitter; +type Options = { + state: State; + emitter: NavigationEventEmitter>; }; /** * Hook to take care of emitting `focus` and `blur` events. */ -export default function useFocusEvents({ state, emitter }: Options) { +export default function useFocusEvents({ + state, + emitter, +}: Options) { const navigation = React.useContext(NavigationContext); const lastFocusedKeyRef = React.useRef(); diff --git a/packages/core/src/useNavigationBuilder.tsx b/packages/core/src/useNavigationBuilder.tsx index 7c98972f..5647c221 100644 --- a/packages/core/src/useNavigationBuilder.tsx +++ b/packages/core/src/useNavigationBuilder.tsx @@ -29,6 +29,7 @@ import { RouteConfig, PrivateValueStore, EventMapBase, + EventMapCore, } from './types'; import useStateGetters from './useStateGetters'; import useOnGetState from './useOnGetState'; @@ -378,7 +379,7 @@ export default function useNavigationBuilder< : (initializedStateRef.current as State); }, [getCurrentState, isStateInitialized]); - const emitter = useEventEmitter((e) => { + const emitter = useEventEmitter>((e) => { let routeNames = []; let route: Route | undefined; diff --git a/packages/core/src/useNavigationCache.tsx b/packages/core/src/useNavigationCache.tsx index 6ec150d1..273b6bf7 100644 --- a/packages/core/src/useNavigationCache.tsx +++ b/packages/core/src/useNavigationCache.tsx @@ -8,7 +8,7 @@ import { } from '@react-navigation/routers'; import type { NavigationEventEmitter } from './useEventEmitter'; -import type { NavigationHelpers, NavigationProp } from './types'; +import type { EventMapBase, NavigationHelpers, NavigationProp } from './types'; type Options = { state: State; @@ -19,7 +19,7 @@ type Options = { cb: (options: Record) => Record ) => void; router: Router; - emitter: NavigationEventEmitter; + emitter: NavigationEventEmitter; }; type NavigationCache< diff --git a/packages/core/src/useNavigationHelpers.tsx b/packages/core/src/useNavigationHelpers.tsx index bde45661..0820d84d 100644 --- a/packages/core/src/useNavigationHelpers.tsx +++ b/packages/core/src/useNavigationHelpers.tsx @@ -20,7 +20,7 @@ type Options = { visitedNavigators?: Set ) => boolean; getState: () => State; - emitter: NavigationEventEmitter; + emitter: NavigationEventEmitter; router: Router; }; diff --git a/packages/routers/src/StackRouter.tsx b/packages/routers/src/StackRouter.tsx index 0b3aedbb..01744914 100644 --- a/packages/routers/src/StackRouter.tsx +++ b/packages/routers/src/StackRouter.tsx @@ -51,7 +51,7 @@ export type StackActionHelpers = { * @param [params] Params object for the new route. */ replace( - ...args: ParamList[RouteName] extends undefined + ...args: undefined extends ParamList[RouteName] ? [RouteName] | [RouteName, ParamList[RouteName]] : [RouteName, ParamList[RouteName]] ): void; @@ -63,7 +63,7 @@ export type StackActionHelpers = { * @param [params] Params object for the route. */ push( - ...args: ParamList[RouteName] extends undefined | any + ...args: undefined extends ParamList[RouteName] ? [RouteName] | [RouteName, ParamList[RouteName]] : [RouteName, ParamList[RouteName]] ): void; diff --git a/packages/routers/src/TabRouter.tsx b/packages/routers/src/TabRouter.tsx index f9042de4..89a19ddf 100644 --- a/packages/routers/src/TabRouter.tsx +++ b/packages/routers/src/TabRouter.tsx @@ -42,7 +42,7 @@ export type TabActionHelpers = { * @param [params] Params object for the route. */ jumpTo>( - ...args: ParamList[RouteName] extends undefined | any + ...args: undefined extends ParamList[RouteName] ? [RouteName] | [RouteName, ParamList[RouteName]] : [RouteName, ParamList[RouteName]] ): void;