react-navigation / hooks

React hooks for convenient react-navigation use
https://reactnavigation.org
MIT License
576 stars 36 forks source link

I can't define NavigationParams types with useNavigation #49

Closed Aure77 closed 4 years ago

Aure77 commented 4 years ago

There is a way to define navigation params typing on useNavigation with typescript ?

  const navigation = useNavigation<{ params: { title: string } }>();
  const title = navigation.getParam("title"); // title is inferred as "any" not "string"...

This is not working useNavigation because of this (P default value/type) :

export interface NavigationScreenProp<S, P = NavigationParams> {
  state: S & { params?: P };
...

there is no access to optional second type on NavigationScreenProp...

slorber commented 4 years ago

Hi,

This looks a bit similar to #42 so I'm closing.

For now the best solution is probably something like useNavigation() as NavigationStackProp<MyParams>

I don't have much time to work on this currently but PRs are welcome