11import * as React from 'react' ;
2- import { CommonActions , type NavigationContainerRef , StackActions } from '@react-navigation/native' ;
2+ import {
3+ CommonActions ,
4+ type NavigationAction ,
5+ type NavigationContainerRef ,
6+ type NavigationState ,
7+ type ParamListBase ,
8+ StackActions
9+ } from '@react-navigation/native' ;
310
411import { type StackParamList } from '../../definitions/navigationTypes' ;
512
613const navigationRef = React . createRef < NavigationContainerRef < StackParamList > > ( ) ;
714const routeNameRef : React . MutableRefObject < NavigationContainerRef < StackParamList > | null > = React . createRef ( ) ;
815
916// Note: name can be a top-level route (keyof StackParamList) or nested route (string)
10- // This allows navigation to both top-level and nested routes
11- function navigate ( name : keyof StackParamList | string , params ?: any ) {
12- navigationRef . current ?. navigate ( name as any , params ) ;
17+ // This allows navigation to both top-level and nested routes. Params are typed when name is a keyof StackParamList.
18+ function navigate < K extends keyof StackParamList | string > (
19+ name : K ,
20+ params ?: K extends keyof StackParamList ? StackParamList [ K ] : object
21+ ) : void {
22+ const nav = navigationRef . current ;
23+ if ( nav ) {
24+ ( nav . navigate as ( n : keyof StackParamList , p ?: StackParamList [ keyof StackParamList ] ) => void ) (
25+ name as keyof StackParamList ,
26+ params as StackParamList [ keyof StackParamList ]
27+ ) ;
28+ }
1329}
1430
1531// Note: name can be a top-level route (keyof StackParamList) or nested route (string)
16- function push ( name : keyof StackParamList | string , params ?: any ) {
17- navigationRef . current ?. dispatch ( StackActions . push ( name as any , params ) ) ;
32+ function push < K extends keyof StackParamList | string > (
33+ name : K ,
34+ params ?: K extends keyof StackParamList ? StackParamList [ K ] : object
35+ ) : void {
36+ navigationRef . current ?. dispatch (
37+ StackActions . push ( name as keyof StackParamList , params as StackParamList [ keyof StackParamList ] )
38+ ) ;
1839}
1940
2041function back ( ) {
2142 navigationRef . current ?. dispatch ( CommonActions . goBack ( ) ) ;
2243}
2344
2445// Note: name can be a top-level route (keyof StackParamList) or nested route (string)
25- function replace ( name : keyof StackParamList | string , params ?: any ) {
26- navigationRef . current ?. dispatch ( StackActions . replace ( name as any , params ) ) ;
46+ function replace < K extends keyof StackParamList | string > (
47+ name : K ,
48+ params ?: K extends keyof StackParamList ? StackParamList [ K ] : object
49+ ) : void {
50+ navigationRef . current ?. dispatch (
51+ StackActions . replace ( name as keyof StackParamList , params as StackParamList [ keyof StackParamList ] )
52+ ) ;
2753}
2854
2955// Pops to the first occurrence of the given route name, usually RoomView
3056// Note: name can be a nested route name (e.g., 'RoomView', 'DrawerNavigator') not just top-level routes
3157function popTo ( name : keyof StackParamList | string ) {
32- navigationRef . current ?. dispatch ( StackActions . popTo ( name as any ) ) ;
58+ navigationRef . current ?. dispatch ( StackActions . popTo ( name as keyof StackParamList ) ) ;
3359}
3460
3561// Removes RoomView from the stack and leaves only RoomsListView open
@@ -55,14 +81,18 @@ function popToRoom(isMasterDetail: boolean) {
5581 }
5682}
5783
58- function dispatch ( params : any ) {
59- navigationRef . current ?. dispatch ( params ) ;
84+ type DispatchAction =
85+ | NavigationAction
86+ | ( ( state : Readonly < NavigationState < ParamListBase > > ) => NavigationAction ) ;
87+
88+ function dispatch ( action : DispatchAction ) {
89+ navigationRef . current ?. dispatch ( action as Parameters < NavigationContainerRef < StackParamList > [ 'dispatch' ] > [ 0 ] ) ;
6090}
6191
6292// Note: screen can be a nested route name (e.g., 'RoomView') not just top-level routes
6393function resetTo ( screen : keyof StackParamList | string = 'RoomView' ) {
64- navigationRef . current ?. dispatch ( ( state : any ) => {
65- const index = state . routes . findIndex ( ( r : any ) => r . name === screen ) ;
94+ navigationRef . current ?. dispatch ( ( state : Readonly < NavigationState < ParamListBase > > ) => {
95+ const index = state . routes . findIndex ( ( r : { name : string } ) => r . name === screen ) ;
6696 const routes = state . routes . slice ( 0 , index + 1 ) ;
6797
6898 return CommonActions . reset ( {
@@ -78,7 +108,7 @@ function getCurrentRoute() {
78108}
79109
80110// Note: params can be for any route, including nested routes
81- function setParams ( params : any ) {
111+ function setParams ( params : object ) {
82112 navigationRef . current ?. setParams ( params ) ;
83113}
84114
0 commit comments