In the typescript-section of react navigation it is mentioned, that we can either annotate all the navigation props in all the screens or if we use a lot of useNavigation() and links we can declare a global namespace and extend the root navigator. My question is can we just use the declare global {} syntax and will it correctly suggest the functions for that navigation, wherever we use the useNavigation hook. My code looks like as given below.
export type RootStackNavigatorParamsList = {
NavigatorC: NavigatorScreenParams<StackNavigatorC>;
NavigatorB: NavigatorScreenParams<StackNavigatorB>;
NavigatorA: NavigatorScreenParams<StackNavigatorA>;
DrawerNavigator: NavigatorScreenParams<DrawerNavigatorParamList>;
};
export type DrawerNavigatorParamList = {
TabNavigator: NavigatorScreenParams<TabNavigatorParamList>;
};
export type TabNavigatorParamList = {
NavigatorD: NavigatorScreenParams<StackNavigatorD>;
NavigatorE: NavigatorScreenParams<StackNavigatorE>;
NavigatorF: NavigatorScreenParams<StackNavigatorF>;
};
declare global {
namespace ReactNavigation {
interface RootParamList extends RootStackNavigatorParamsList {}
}
}
Now inside ScreenF in NavigatorF when I call navigation.toggleDrawer(). It gives me the type error that :
Property ‘toggleDrawer’ does not exist on type
‘Omit<NavigationProp, “getState”> & { getState():
Readonly<{ key: string; index: number; routeNames: string[]; history?:
unknown[] | undefined; routes: NavigationRoute<…>[]; type: string;
stale: false; }> | undefined; }’.
Here navigation = useNavigation()
But when I explicitly define the type for this screen and use it then its fine
type ScreenFProps<T extends keyof StackNavigatorF> = CompositeScreenProps<
CompositeScreenProps<
CompositeScreenProps<
StackScreenProps<StackNavigatorF, T>,
BottomTabScreenProps<TabNavigatorParamList>
>,
DrawerScreenProps<DrawerNavigatorParamList>
>,
StackScreenProps<RootStackNavigatorParamsList>
>;
navigation =
useNavigation<HomeScreenProps<"HomeScreen">["navigation"]>();
Will redeclaring the global namespace not start suggesting the methods correctly, as is implied in the docs or am I doing something wrong?