Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct TypeScript type for useLinkProps To #12013

Closed
4 of 7 tasks
MadeinFrance opened this issue Jun 10, 2024 · 4 comments
Closed
4 of 7 tasks

Correct TypeScript type for useLinkProps To #12013

MadeinFrance opened this issue Jun 10, 2024 · 4 comments

Comments

@MadeinFrance
Copy link

MadeinFrance commented Jun 10, 2024

Current behavior

Hi, I'm trying to use To in an external package but I lose the App typings define in the App folder as:

// app/navigation.d.ts

type Stack = MyRouterStack;

declare global {
  namespace ReactNavigation {
    interface RootParamList extends Stack {}
  }
}

// package/components/core/src/link/link.tsx

import {useLinkProps, type NavigationAction, type Link as NavLink} from '@react-navigation/native';

import React, {forwardRef, memo, useCallback} from 'react';
import {type TouchableOpacity} from 'react-native-gesture-handler';

import {BaseLink, type BaseLinkProps} from './base-link';

export interface LinkProps extends BaseLinkProps {
  to: React.ComponentProps<typeof NavLink>['to'];
  action?: NavigationAction;
}

export const Link = memo(
  forwardRef<TouchableOpacity, LinkProps>(function Link(
    {to, action, onPress, ...rest},
    ref,
  ) {
    const {onPress: linkOnPress} = useLinkProps({to, action});

    const pressHandler = useCallback(() => {
      linkOnPress();

      onPress?.();
    }, [linkOnPress, onPress]);

    return <BaseLink ref={ref} onPress={pressHandler} {...rest} />;
  }),
);

Expected behavior

to props should be strongly typed to MyRouterStack.

// No TypeScript error:

<Link
  to={{
    screen: 'some_incorrect_screen_string_name',
  }}
/>

Reproduction

https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbwK4GcCmAZYA7A1gBSgjBQBo4YBPMNOAOQEMA3YAcwZmAmwEEBjTtwC+cAGZEQcAOQABKGgYCAtNmZsOXbAHpVnJmikBuAFChIsCtVoIAKhBHiIk2fMUwVa9oO27g+rQA2wABGWlQ0KHxQwGAwWihQfFqomDi4dkam4NDwAEoKAuQIotAA7gxQACb5ouQgaCAQ5CkAwgwBAcGKuA4S0q4CmWY5iOG0dkh8ABYMwQFoAPJgisBUvU79Be6++kqsaCgwSPJKM9iV81CZWebwCABCDOhYeORjcI-PaYTEKOvOADotF10EognhrmgAB63OA4GBoKCiRS0F4EIgkODQhHnFAfJ6pPA-TEIYxwCgQABccDsJnJbk0AH5qYwWF5NPxvCYhMZjNDYXxuIc4Gi4ABeOD1RoACjJYjKFWqaFEAB4JtNZvMlisqOQ0cSUAA+aWiJDYASaEVpWXk8kIGBNOAM7jkbiEA5kOAA73yQ5CUhy8nyWpygCUiEDcEF2GFCDdvpQ1PBuAW2HdKD+4rgKX1GJQ0vtjud2CEoZMkejwrACYAEgxzpcs612p1utLpeGxYaI7bbcnU+n82Xeb3yfGPYyAR26bb-XAANr9tMJ13Lj0AXWHkfkRyg2DgKs+hNwcGDYoQwZE44z5+rHrrDcRIgQ3oBvpgIi0hpnpYDw6AA

Platform

  • Android
  • iOS
  • Web
  • Windows
  • MacOS

Packages

  • @react-navigation/native

Environment

  • I've removed the packages that I don't use
package version
@react-navigation/native 6.1.17
Copy link

Hey @MadeinFrance! Thanks for opening the issue. It seems that the issue doesn't contain a link to a repro.

The best way to get attention to your issue is to provide an easy way for a developer to reproduce the issue.

You can provide a repro using any of the following:

A snack link is preferred since it's the easiest way to both create and share a repro. If it's not possible to create a repro using a snack, link to a GitHub repo under your username is a good alternative. Don't link to a branch or specific file etc. as it won't be detected.

Try to keep the repro as small as possible by narrowing down the minimal amount of code needed to reproduce the issue. Don't link to your entire project or a project containing code unrelated to the issue. See "How to create a Minimal, Reproducible Example" for more information.

You can edit your original issue to include a link to the repro, or leave it as a comment. The issue will be closed automatically after a while if you don't provide a repro.

Copy link

Couldn't find version numbers for the following packages in the issue:

  • @react-navigation/native
  • @react-navigation/stack
  • @react-navigation/bottom-tabs
  • @react-navigation/drawer
  • @react-navigation/material-top-tabs
  • react-native-tab-view

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

Copy link

Couldn't find version numbers for the following packages in the issue:

  • @react-navigation/stack

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

The versions mentioned in the issue for the following packages differ from the latest versions on npm:

  • @react-navigation/native (found: 6.1.14, latest: 6.1.17)

Can you verify that the issue still exists after upgrading to the latest versions of these packages?

Copy link

Couldn't find version numbers for the following packages in the issue:

  • @react-navigation/stack

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant