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
Copy file name to clipboardExpand all lines: versioned_docs/version-7.x/typescript.md
+127-1Lines changed: 127 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,130 @@ title: Type checking with TypeScript
4
4
sidebar_label: Type checking with TypeScript
5
5
---
6
6
7
-
React Navigation is written with TypeScript and exports type definitions for TypeScript projects.
7
+
import Tabs from '@theme/Tabs';
8
+
import TabItem from '@theme/TabItem';
9
+
10
+
React Navigation can be configured to type-check screens and their params, as well as various other APIs using TypeScript. This provides better intelliSense and type safety when working with React Navigation.
11
+
12
+
<TabsgroupId="config"queryString="config">
13
+
<TabItemvalue="static"label="Static"default>
14
+
15
+
There are 2 steps to configure TypeScript with the static API:
16
+
17
+
1. Each screen component needs to specify the type of the `route.params` prop that it accepts. The `StaticScreenProps` type makes it simpler:
This is needed to type-check the `useNavigation` hook.
59
+
60
+
## Navigator specific types
61
+
62
+
Generally we recommend using the default types for the `useNavigation` prop to access the navigation object in a navigator agnostic manner. However, if you need to use navigator specific APIs, you need to manually annotate `useNavigation`:
Here, the `HomeTabs` component is defined using the dynamic API. This means that when we create the param list for the root navigator with `StaticParamList<typeof RootStack>`, it won't know about the screens defined in the nested navigator. To fix this, we'd need to specify the param list for the nested navigator explicitly.
102
+
103
+
This can be done by using the type of the `route` prop that the screen component receives:
Now, when using `StaticParamList<typeof RootStack>`, it will include the screens defined in the nested navigator.
126
+
127
+
</TabItem>
128
+
<TabItemvalue="dynamic"label="Dynamic">
129
+
130
+
When using the dynamic API, it is necessary to specify the types for each screen as well as the nesting structure as it cannot be inferred from the code.
0 commit comments