React Navigation with TypeScript

Astha Korat
Yudiz Solutions
Published in
3 min readApr 16, 2024

In TypeScript navigation, developers define types for navigation parameters, screen props, and navigation props. By explicitly typing these elements, TypeScript helps prevent type-related errors and provides better code documentation and readability. TypeScript’s type system ensures that only valid navigation actions and parameters are used throughout the application, reducing the risk of runtime errors and improving overall code quality and maintainability.

  • TypeScript navigation in React Native combines the flexibility and power of React Navigation with the safety and clarity provided by TypeScript’s static type checking, resulting in more reliable and robust navigation flows within React Native applications.
  • Difference Between Typescript Navigation and Without Using TypeScript Navigation

1.Type checking the navigator

  • The initial step is to establish an object type that maps route names to their corresponding parameters. In our case, our Screen1 doesn’t depend on any params, so we’d map the route name to undefined and Screen2 has two params so defined with types.
  • importing and adding StackParamList type inside the Stack.

2.Type Checking the individual Screen

  • First of all, create two simple screens by creating two files- Screen1.tsx and Screen2.tsx.
  • The code of Screen1.tsx and Screen2.tsx is given below.
  • How to Share Data Between Screens in React Native Using TypeScript
  • Now, we have to add type checks. Create a new file named types.tsx
  • The React Navigation navigator packages provide a type that helps specify the types for navigation and route properties within each navigator. This makes it easier to define and manage navigation-related data in your app.
  • For example, you can use BottomTabScreenProps for the Bottom Navigator (@react-navigation/bottom-tabs).
  • For our case, we’d use NativeStackScreenProps for the Native Stack Navigator.
  • Modify the App.tsx file by importing and incorporating the StackParamList type.
  • As you see, we intend to pass the username and age data from Screen1 to Screen2. Now, modify the Screen1.tsx file.
  • Now see Screen2.tsx.
  • Inside the Screen2 types, we have not added ‘phoneNumber’ that’s why they give an error.

Summary:

Implementing React Navigation with TypeScript offers significant benefits in terms of code safety and readability. By explicitly defining types for navigation parameters, screen props, and navigation props, TypeScript helps prevent type-related errors and improves code documentation.

The combination of React Navigation’s flexibility with TypeScript’s static type checking ensures more reliable and robust navigation flows in React Native applications. TypeScript navigation improves code maintainability by ensuring type correctness across the codebase, leading to clearer code and better IDE support. Additionally, TypeScript navigation simplifies data sharing between screens, ensuring type correctness and enhancing code safety throughout the application.

--

--