Typing a function that receives page and params #11982
Replies: 1 comment 1 reply
-
Your logic is not correct. The function you wrote has Not sure why you're trying to implement a function that is the same as const navigateToRoute = <RouteName extends keyof RootStackParamList>(
...args: RouteName extends unknown
? undefined extends RootStackParamList[RouteName]
?
| [screen: RouteName]
| [screen: RouteName, params: RootStackParamList[RouteName]]
: [screen: RouteName, params: RootStackParamList[RouteName]]
: never
) => {
navigation.navigate<RouteName>(...args);
}; |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello, I'm creating a function to be used in the Navbar. This function takes page and params as arguments and navigates to the desired page. I worked on it with ChatGPT, but there's a part that's not working properly, so I'm asking for your opinion.
Given the RootStackParamList type above, I created a function as follows:
However, I'm getting an error that the routeName and params in the navigation.navigate() part do not match the types. It seems that this is happening because the generic type is not being inferred correctly. How can I fix this?
Below is the error.
I haven't been able to work this out for days now, get me out of hell
Beta Was this translation helpful? Give feedback.
All reactions