react-navigation / react-navigation/react-navigation.github.io

Inconsistent TypeScript behavior with optional arguments in useNavigation

Open
#1,232 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
324
Forks
2k
PR merge metrics
No merged PRs in 30d

Description

When using the useNavigation hook from @react-navigation/native in TypeScript, optional arguments are not being correctly inferred and produce a type error.

Expected Behavior:

Optional arguments should be correctly inferred and not produce a type error.

Actual Behavior:

Optional arguments produce a type error, requiring the use of the // @ts-ignore comment to suppress the error.

Steps to Reproduce:

  • Use the useNavigation hook with an optional argument.
  • Attempt to pass a string value as the optional argument.
  • Observe the type error.
import { useNavigation } from '@react-navigation/native';

const navigation = useNavigation();

const navigate = (screenName?: string) => {
  // Argument of type 'string' is not assignable to parameter of type 'never'
  navigation.navigate(screenName)
  // Workaround with // @ts-ignore
  // @ts-ignore
  navigation.navigate(screenName)
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the TypeScript error in the useNavigation example from the issue, then inspect the useNavigation entry point and its type definitions in @react-navigation/native. Done means an optional string argument is accepted without requiring a @ts-ignore comment, while navigation typing remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, react-native, typescript
Domain
api, mobile-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.