react-navigation/react-navigation.github.io

Inconsistent TypeScript behavior with optional arguments in useNavigation

ThePlatinum opened this issue · 0 comments

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)
}