Not possible to type annotate expected params for navigation.navigate
TL;DR: navigation.push, navigation.navigate, etc. should be type annotated in a generic way that allows the params argument to be overridden with a narrower type.
E.g. when I call navigation.navigate('Details', { title: 'Hello world' }), I would like the second argument (params) to be type checked against an interface of my own choosing. This is not currently possible.
Pull Request (1 of 2) Pull Request (2 of 2): TODO
Current Behavior
Per the documentation on Type checking with TypeScript, the navigation param is annotated with the NavigationStackProp interface which inherits from the NavigationScreenProp interface. In both of these interfaces the family of navigation methods (push, navigate, etc) all annotate the params argument as the NavigationParams interface. NavigationParams is typed as [key: string]: any; which only enforces that params is an object literal with strings for keys.
Expected Behavior
It should be possible to override the type annotation for navigation params using generics.
How to reproduce
Observe the linked declaration files above. Params are always hardcoded as NavigationParams.
Your Environment
n/a
There are currently two open PRs to address this: https://github.com/react-navigation/react-navigation/pull/6444 https://github.com/react-navigation/react-navigation/pull/6515
Hello,
I think we should also type the first param routeName.
I suggest for React Navigation Stack:
Add RouteName type to NavigationScreenProp and replace routeName: string by routeName: RouteName:
(routeNameOrOptions take also "options", I didn't info about this)
export interface NavigationScreenProp<S, P = NavigationParams, RouteName = string> {
state: S & { params?: P };
dispatch: NavigationDispatch;
goBack: (routeKey?: string | null) => boolean;
dismiss: () => boolean;
navigate(options: {
routeName:
| RouteName
| {
routeName: RouteName;
params?: NavigationParams;
action?: NavigationNavigateAction;
key?: string;
};
params?: NavigationParams;
action?: NavigationAction;
key?: string;
}): boolean;
navigate(
routeNameOrOptions: RouteName,
params?: NavigationParams,
action?: NavigationAction
): boolean;
openDrawer: () => any;
closeDrawer: () => any;
toggleDrawer: () => any;
getParam<T extends keyof P>(
param: T,
fallback: NonNullable<P[T]>
): NonNullable<P[T]>;
getParam<T extends keyof P>(param: T): P[T];
setParams: (newParams: Partial<P>) => boolean;
emit: (eventName: 'refocus') => void;
addListener: (
eventName: string,
callback: NavigationEventCallback
) => NavigationEventSubscription;
isFocused: () => boolean;
isFirstRouteInParent: () => boolean;
router?: NavigationRouter;
dangerouslyGetParent: () => NavigationScreenProp<S> | undefined;
}
Add RouteName type to NavigationStackScreenProps and put it in NavigationStackProp :
export declare type NavigationStackScreenProps<Params = NavigationParams, ScreenProps = unknown, RouteName = string> = {
theme: SupportedThemes;
navigation: NavigationStackProp<NavigationRoute, Params, RouteName>;
screenProps: ScreenProps;
};
Add RouteName type to NavigationStackProp and put it in NavigationScreenProp :
export declare type NavigationStackProp<State = NavigationRoute, Params = NavigationParams, RouteName = string> = NavigationScreenProp<State, Params, RouteName> & {
push: (routeName: RouteName, params?: NavigationParams, action?: NavigationNavigateAction) => boolean;
replace: (routeName: RouteName, params?: NavigationParams, action?: NavigationNavigateAction) => boolean;
reset: (actions: NavigationAction[], index: number) => boolean;
pop: (n?: number, params?: {
immediate?: boolean;
}) => boolean;
popToTop: (params?: {
immediate?: boolean;
}) => boolean;
addListener: (event: NavigationStackEventName, callback: NavigationEventCallback) => NavigationEventSubscription;
};
Finally the usage:
type RouteName = "ScreenA" | "ScreenB";
type Props = NavigationStackScreenProps<undefined, undefined, RouteName>;
class ScreenA extends React.Component<Props, undefined> {
render() {
return (
<View>
<Link onPress={() => this.props.navigation.navigate("ScreenB")}>Navigate to screen B</Link>
</View>
);
}
}
Thanks!
I think navigation.navigate, push etc. should be generic types, because maybe we want to navigate to different routes.
Closing this as it seems to be resolved in latest version.
Hey! This issue is closed and isn't watched by the core team. You are welcome to discuss the issue with others in this thread, but if you think this issue is still valid and needs to be tracked, please open a new issue with a repro.