Skip to content

Commit 476ffe7

Browse files
committed
fix: replace any types with proper navigation stack types in navigation refs (#6806)
- appNavigation.ts: type navigate, push, replace params from StackParamList; type dispatch, resetTo, setParams - navigationTypes.ts: INavigationProps use RouteProp and NativeStackNavigationProp instead of any
1 parent 21dfe53 commit 476ffe7

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

app/definitions/navigationTypes.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import { type NavigatorScreenParams } from '@react-navigation/core';
2-
import { type NativeStackNavigationOptions } from '@react-navigation/native-stack';
1+
import { type NavigatorScreenParams, type ParamListBase } from '@react-navigation/core';
2+
import { type RouteProp } from '@react-navigation/native';
3+
import { type NativeStackNavigationOptions, type NativeStackNavigationProp } from '@react-navigation/native-stack';
34

45
import { type TSubscriptionModel } from './ISubscription';
56
import { type TServerModel } from './IServer';
67
import { type IAttachment } from './IAttachment';
78
import { type MasterDetailInsideStackParamList } from '../stacks/MasterDetailStack/types';
89
import { type OutsideParamList, type InsideStackParamList } from '../stacks/types';
910

10-
interface INavigationProps {
11-
route?: any;
12-
navigation?: any;
11+
export interface INavigationProps {
12+
route?: RouteProp<ParamListBase, string>;
13+
navigation?: NativeStackNavigationProp<ParamListBase, string>;
1314
isMasterDetail?: boolean;
1415
}
1516

app/lib/navigation/appNavigation.ts

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,61 @@
11
import * 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

411
import { type StackParamList } from '../../definitions/navigationTypes';
512

613
const navigationRef = React.createRef<NavigationContainerRef<StackParamList>>();
714
const 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

2041
function 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
3157
function 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
6393
function 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

Comments
 (0)