You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here, the `StaticParamList` utility type generates the param list type from the navigator config. Declaring the global `RootParamList` interface enables type checking for [`useNavigation`](use-navigation.md), [`Link`](link.md) etc.
614
+
Declaring the `RootNavigator` module augmentation lets React Navigation know about the navigator used at the root of your app. This enables type checking for [`useNavigation`](use-navigation.md), [`Link`](link.md) etc.
617
615
618
616
See [Configuring TypeScript](typescript.md) for more details.
@@ -369,7 +365,7 @@ type ProfileScreenNavigationProp = CompositeNavigationProp<
369
365
:::danger
370
366
371
367
Annotating `useNavigation` isn't type-safe because the type parameter cannot be statically verified.
372
-
Prefer [specifying a default type](#specifying-default-types-for-usenavigation-link-ref-etc) instead.
368
+
Prefer [specifying the type of your root navigator](#specifying-default-types-for-usenavigation-link-ref-etc) instead.
373
369
374
370
:::
375
371
@@ -473,19 +469,22 @@ const navigationRef =
473
469
474
470
## Specifying default types for `useNavigation`, `Link`, `ref` etc
475
471
476
-
Instead of manually annotating these APIs, you can specify a global type for your root navigator which will be used as the default type.
472
+
Instead of manually annotating these APIs, you can specify the type of your root navigator, which will be used to infer the default types.
477
473
478
-
To do this, you can add this snippet somewhere in your codebase:
474
+
To do this, you can use module augmentation for `@react-navigation/core` and extend the `RootNavigator` interface with the type of your root navigator:
The `RootParamList` interface lets React Navigation know about the params accepted by your root navigator. Here we extend the type `RootStackParamList` because that's the type of params for our stack navigator at the root. The name of this type isn't important.
487
+
This lets React Navigation know about the navigator used at the root of your app. Here we extend the `RootNavigator` interface with `RootStackType` because that's the type of the stack navigator at the root. The name of this type isn't important.
489
488
490
489
Specifying this type is important if you heavily use [`useNavigation`](use-navigation.md), [`Link`](link.md) etc. in your app since it'll ensure type-safety. It will also make sure that you have correct nesting on the [`linking`](navigation-container.md#linking) prop.
491
490
@@ -495,7 +494,7 @@ When writing types for React Navigation, there are a couple of things we recomme
495
494
496
495
1. It's good to create a separate file (e.g. `navigation/types.tsx`) that contains the types related to React Navigation.
497
496
2. Instead of using `CompositeNavigationProp` directly in your components, it's better to create a helper type that you can reuse.
498
-
3. Specifying a global type for your root navigator would avoid manual annotations in many places.
497
+
3. Specifying the type of your root navigator avoids manual annotations in many places.
499
498
500
499
Considering these recommendations, the file containing the types may look something like this:
With this change, you'd now have full type-safety when using the `Link` component given that you have [configured the global type](typescript.md#specifying-default-types-for-usenavigation-link-ref-etc).
212
+
With this change, you'd now have full type-safety when using the `Link` component given that you have [configured the type of your root navigator](typescript.md#specifying-default-types-for-usenavigation-link-ref-etc).
213
213
214
214
See [`Link`](link.md) and [`useLinkProps`](use-link-props.md) for usage.
Copy file name to clipboardExpand all lines: versioned_docs/version-8.x/upgrading-from-7.x.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,7 +61,7 @@ We introduced a static API in React Navigation 7. However, some of the TypeScrip
61
61
62
62
#### The root type now uses navigator type instead of param list
63
63
64
-
Previously the types for the root navigator were specified using `declare global` and `RootParamList`. Now, they can be specified with module augmentation of `@react-navigation/core` and use the navigator's type instead a param list:
64
+
Previously the types for the root navigator were specified using `declare global` and `RootParamList`. Now, they can be specified with module augmentation of `@react-navigation/core` and use the navigator's type instead of a param list:
65
65
66
66
```diff lang=ts
67
67
- type RootStackParamList = StaticParamList<typeof RootStack>;
0 commit comments