Skip to content

Commit f3b138d

Browse files
committed
Tweak docs for TypeScript in 8.x
1 parent f43d3ac commit f3b138d

1 file changed

Lines changed: 20 additions & 14 deletions

File tree

versioned_docs/version-8.x/typescript.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,24 @@ After setting up the type for the root navigator, all we need to do is specify t
5757

5858
This can be done in 2 ways:
5959

60-
1. The type annotation for the component specified in `screen`:
60+
1. The path pattern specified in the linking config (e.g. for `path: 'profile/:userId'`, the type of `route.params` is `{ userId: string }`). The type can be further customized by using a [`parse` function in the linking config](configuring-links.md#passing-params):
61+
62+
```ts
63+
linking: {
64+
// highlight-start
65+
path: 'profile/:userId',
66+
parse: {
67+
userId: (id) => parseInt(id, 10),
68+
},
69+
// highlight-end
70+
},
71+
```
72+
73+
The above example would make the type of `route.params` be `{ userId: number }` since the `parse` function converts the string from the URL to a number.
74+
75+
This is the recommended way to specify params for screens that are accessible via deep linking or if your app runs on the Web, as it ensures that the types of params are consistent with the URL.
76+
77+
2. The type annotation for the component specified in `screen`:
6178

6279
```ts
6380
import type { StaticScreenProps } from '@react-navigation/native';
@@ -85,19 +102,10 @@ This can be done in 2 ways:
85102
}
86103
```
87104

88-
2. The path pattern specified in the linking config (e.g. for `linking: 'profile/:userId'`, the type of `route.params` is `{ userId: string }`). The type can be further customized by using a [`parse` function in the linking config](configuring-links.md#passing-params):
89-
90-
```ts
91-
linking: {
92-
path: 'profile/:userId',
93-
parse: {
94-
userId: (id) => parseInt(id, 10),
95-
},
96-
},
97-
```
98-
99105
The above example would make the type of `route.params` be `{ userId: number }` since the `parse` function converts the string from the URL to a number.
100106

107+
If your app supports deep linking or runs on the Web, you can use this pattern to specify any additional optional params that don't appear in the path pattern (e.g. query params). Make sure to add `| undefined` to the type of params to make them optional, as query params may not be present in the URL.
108+
101109
If both `screen` and `linking` specify params, the final type of `route.params` is the intersection of both types.
102110

103111
This is how the complete example would look like:
@@ -120,8 +128,6 @@ const MyStack = createNativeStackNavigator({
120128
});
121129
```
122130

123-
If your app supports deep linking or runs on the Web, it is recommended to specify params that appear in the path pattern in the linking config. Any additional params (e.g. query params) can be specified in the component's props.
124-
125131
If you have specified the params in `linking`, it's recommended to not specify them again in the component's props, and use `useRoute('ScreenName')` instead to get the correctly typed `route` object.
126132

127133
The `createXScreen` helper functions enable type inference in screen configuration callbacks like `options`, `listeners`, etc. Each navigator exports its own version of the helper function:

0 commit comments

Comments
 (0)