gatsby icon indicating copy to clipboard operation
gatsby copied to clipboard

[TS] Link is incompatible with React.forwardRef due to ref type mismatch

Open aaronadamsCA opened this issue 3 years ago β€’ 17 comments

Preliminary Checks

  • [X] This issue is not a duplicate. Before opening a new issue, please search existing issues: https://github.com/gatsbyjs/gatsby/issues
  • [X] This issue is not a question, feature request, RFC, or anything other than a bug report directly related to Gatsby. Please post those things in GitHub Discussions: https://github.com/gatsbyjs/gatsby/discussions

Description

In a TypeScript environment, Gatsby' Link component is not compatible with React.forwardRef due to a ref type mismatch.

Reproduction Link

https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBDAnmApnA3nA4gQxgZwCNEAZYAOwGsAFKCMfOAXzgDM6Q4AiAczyMRcA3AChQkWBjhkqzNh258CxYWPDR4AJRQ4AxjAA0U1tADuOKABNtrOewicuUHftUiUADwnwkqOAFlSCho6BjgAXmx+YhkQ+nwAHgBXKnIIU3IAPlF3Lw04XQhyfHhA2Ii2MwtrFFYEgAkAFX8SAEFyXQALaABRABsUEBRyQwCgqlp4zIAKETg2FP1gIrHY6bBQ-CNnVgBKDDn5uGcYJKhyOATy9AA6O434lh3w9B2WPRhgADcUAGE+nD4fAAORwQ3CXA+3xQXDgAHpsocmCJdkIgA

Steps to Reproduce

Run this code:

import type { GatsbyLinkProps } from "gatsby";
import { Link } from "gatsby";
import React, { forwardRef } from "react";

export type MyLinkProps = GatsbyLinkProps<unknown>;

export const MyLink = forwardRef<HTMLAnchorElement, MyLinkProps>(
  function MyLink(props, ref) {
    return <Link {...props} ref={ref} activeClassName="active" />;
  }
);

Expected Result

Works normally.

Actual Result

TypeScript complains about a type mismatch on ref={ref}:

No overload matches this call.
  Overload 1 of 2, '(props: GatsbyLinkProps<unknown> | Readonly<GatsbyLinkProps<unknown>>): GatsbyLink<unknown>', gave the following error.
    Type 'ForwardedRef<HTMLAnchorElement>' is not assignable to type '((string | ((instance: GatsbyLink<unknown> | null) => void) | RefObject<GatsbyLink<unknown>>) & (string | ((instance: HTMLAnchorElement | null) => void) | RefObject<...>)) | null | undefined'.
      Type '(instance: HTMLAnchorElement | null) => void' is not assignable to type '((string | ((instance: GatsbyLink<unknown> | null) => void) | RefObject<GatsbyLink<unknown>>) & (string | ((instance: HTMLAnchorElement | null) => void) | RefObject<...>)) | null | undefined'.
        Type '(instance: HTMLAnchorElement | null) => void' is not assignable to type 'string & ((instance: HTMLAnchorElement | null) => void)'.
          Type '(instance: HTMLAnchorElement | null) => void' is not assignable to type 'string'.
  Overload 2 of 2, '(props: GatsbyLinkProps<unknown>, context: any): GatsbyLink<unknown>', gave the following error.
    Type 'ForwardedRef<HTMLAnchorElement>' is not assignable to type '((string | ((instance: GatsbyLink<unknown> | null) => void) | RefObject<GatsbyLink<unknown>>) & (string | ((instance: HTMLAnchorElement | null) => void) | RefObject<...>)) | null | undefined'.

My workaround to date depends on stripping ref from GatsbyLinkProps, and then passing my forwarded ref to deprecated prop innerRef, like so:

import type { GatsbyLinkProps } from "gatsby";
import { Link } from "gatsby";
import React, { forwardRef } from "react";

export type MyLinkProps = Omit<GatsbyLinkProps<unknown>, "ref">;

export const MyLink = forwardRef<HTMLAnchorElement, MyLinkProps>(
  function MyLink(props, ref) {
    return <Link {...props} innerRef={ref} activeClassName="active" />;
  }
);

But this wasn't much fun to figure out, nor should it be necessary. Gatsby's Link should be directly compatible with forwardRef out of the box.

Among other things, this would make it possible to pass component={Link} when using MUI. MUI is a (the?) leading React front end framework, and improving compatibility here would be a big plus.

Environment

Gatsby 4.4.0.

aaronadamsCA avatar Dec 27 '21 15:12 aaronadamsCA

Hiya!

This issue has gone quiet. Spooky quiet. πŸ‘»

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! πŸ’ͺπŸ’œ

github-actions[bot] avatar Feb 20 '22 12:02 github-actions[bot]

Not stale.

aaronadamsCA avatar Feb 28 '22 14:02 aaronadamsCA

Hiya!

This issue has gone quiet. Spooky quiet. πŸ‘»

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! πŸ’ͺπŸ’œ

github-actions[bot] avatar Mar 24 '22 00:03 github-actions[bot]

Not stale.

aaronadamsCA avatar Apr 05 '22 10:04 aaronadamsCA

Hopefully this can be addressed as part of PR #22027, though there hasn't been much progress there.

bourn404 avatar Apr 06 '22 16:04 bourn404

Hiya!

This issue has gone quiet. Spooky quiet. πŸ‘»

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! πŸ’ͺπŸ’œ

github-actions[bot] avatar Apr 27 '22 00:04 github-actions[bot]

Still not stale!

aaronadamsCA avatar Apr 28 '22 13:04 aaronadamsCA

Hiya!

This issue has gone quiet. Spooky quiet. πŸ‘»

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! πŸ’ͺπŸ’œ

github-actions[bot] avatar May 19 '22 12:05 github-actions[bot]

πŸ‘»

aaronadamsCA avatar May 19 '22 13:05 aaronadamsCA

Hiya!

This issue has gone quiet. Spooky quiet. πŸ‘»

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! πŸ’ͺπŸ’œ

github-actions[bot] avatar Jul 09 '22 00:07 github-actions[bot]

Not stale.

aaronadamsCA avatar Jul 11 '22 21:07 aaronadamsCA

Hiya!

This issue has gone quiet. Spooky quiet. πŸ‘»

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! πŸ’ͺπŸ’œ

github-actions[bot] avatar Aug 01 '22 00:08 github-actions[bot]

😐

aaronadamsCA avatar Aug 01 '22 06:08 aaronadamsCA

+1

HashimMufti avatar Aug 06 '22 19:08 HashimMufti

++

NitsanCohen770 avatar Aug 25 '22 12:08 NitsanCohen770

Hiya!

This issue has gone quiet. Spooky quiet. πŸ‘»

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! πŸ’ͺπŸ’œ

github-actions[bot] avatar Sep 15 '22 00:09 github-actions[bot]

Not stale

LucidCarson avatar Sep 15 '22 17:09 LucidCarson

Hiya!

This issue has gone quiet. Spooky quiet. πŸ‘»

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! πŸ’ͺπŸ’œ

github-actions[bot] avatar Oct 06 '22 00:10 github-actions[bot]

Hey again!

It’s been 60 days since anything happened on this issue, so our friendly neighborhood robot (that’s me!) is going to close it. Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m HUMAN_EMOTION_SORRY. Please feel free to comment on this issue or create a new one if you need anything else. As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks again for being part of the Gatsby community! πŸ’ͺπŸ’œ

github-actions[bot] avatar Nov 15 '22 12:11 github-actions[bot]

@github-actions this is not stale. Current WIP PR by @Kornil is at https://github.com/gatsbyjs/gatsby/pull/37543 (Thanks Kornil!).

tordans avatar Feb 16 '23 07:02 tordans

any update? I face same problem

icefee avatar Sep 15 '23 01:09 icefee

same here

paolooo avatar Oct 01 '23 07:10 paolooo

Still running into this issue

wg4568 avatar Oct 22 '23 21:10 wg4568