I too was getting the following ts error when not annotating useNavigation
and trying to call navigation.navigate('OtherJazzyScreen
)`:
Argument of type 'string' is not assignable to parameter of type '{ key: string; params?: undefined; merge?: boolean | undefined; } | { name: never; key?: string | undefined; params: never; merge?: boolean | undefined; }'
The OP doesn't specify which version of react-navigation they're using but you can fix this globally in React Navigation 6 which means you then don't have to annotate useNavigation
directly but you still get autocomplete and type checking.
Taken from the react-navigation blog in August 2021 (https://reactnavigation.org/blog/2021/08/14/react-navigation-6.0/#better-type-safety):
In React Navigation 6, you don’t need to annotate useNavigation
to
get autocompletion and type checking. This is possible by defining a
type for the screens globally using declaration merging:
declare global {
namespace ReactNavigation {
interface RootParamList {
Home: undefined;
Profile: { userId: string };
NotFound: undefined;
}
}
}
You can read more about it in our TypeScript
docs.
Another example from the React Navigation 6 docs, if you already have your params declared elsewhere:
// E.g. RootStackParamList.ts
export type RootStackParamList = {
Home: undefined;
Profile: { userId: string };
NotFound: undefined;
};
// E.g. App.tsx
import { RootStackParamList } from 'path/to/RootStackParamList';
declare global {
namespace ReactNavigation {
interface RootParamList extends RootStackParamList {}
}
}