question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[TypeScript] Can't use <Link /> with Gatsby <Link />

See original GitHub issue

I want to use the Gatsby Link component together with Chakras Link component. Unfortunately it seems not to work as expected because I’m getting compile errors:

import React from 'react';
import { Link as GatsbyLink } from 'gatsby';
import { Link } from '@chakra-ui/core';

export function Test() {
  return (
    <Link as={GatsbyLink} to="/me/">
      Me
    </Link>
  );
}

produces a

Type 'typeof GatsbyLink' is not assignable to type '"symbol" | "object" | "a" | "abbr" | "address" | "area" | "article" | "aside" | "audio" | "b" | "base" | "bdi" | "bdo" | "big" | "blockquote" | "body" | "br" | "button" | "canvas" | ... 157 more ... | undefined'.
  Type 'typeof GatsbyLink' is not assignable to type 'ComponentClass<any, any> & string'.
    Type 'typeof GatsbyLink' is not assignable to type 'string'.ts(2322)
index.d.ts(154, 3): The expected type comes from property 'as' which is declared here on type 'IntrinsicAttributes & ILink & IPseudoBoxProps & RefAttributes<HTMLElement> & HTMLAttributes<HTMLElement> & LayoutProps & ColorProps & SpaceProps<ReactText> & ... 16 more ... & { ...; }'

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

5reactions
kripodcommented, Oct 27, 2019

@alexluong You may opt out of type checking for an entire file with // @ts-ignore, but I wouldn’t recommend that.

An other choice is to create an alias for Link like below:

import { Link as ChakraLink } from ‘@chakra-ui/core’;

const Link = ChakraLink as any;

Personally, I’m using Chakra with the router of Gatsby and it made sense to create a custom Link component, using the first approach:

import { Link as ChakraLink, LinkProps } from '@chakra-ui/core';
import { Link as GatsbyLink } from 'gatsby';
import React from 'react';

export default function Link({ href, isExternal, ...restProps }: LinkProps) {
  return (
    // TODO: Don't opt out from type checking once `ChakraLink` gets fixed
    // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
    // @ts-ignore
    <ChakraLink
      {...(isExternal ? { href } : { as: GatsbyLink, to: href })}
      {...restProps}
    />
  );
}
2reactions
tonycommented, May 25, 2020

Thank you @kripod !

As of typescript-eslint 3.0.0, I believe the command will now be ban-ts-comment instead of ban-ts-ignore

import { Link as ChakraLink, LinkProps } from '@chakra-ui/core';
import { Link as GatsbyLink } from 'gatsby';
import React from 'react';

export default function Link({ href, isExternal, ...restProps }: LinkProps) {
  return (
    // TODO: Don't opt out from type checking once `ChakraLink` gets fixed
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    <ChakraLink
      {...(isExternal ? { href } : { as: GatsbyLink, to: href })}
      {...restProps}
    />
  );
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Extending Gatsby Link in Typescript - Polynique
The Gatsby Link Typescript problem​​ This means that Link is exported with the forwardRef property which gives access to the ref property, which ......
Read more >
Wrapping Gatsby's <Link> with TypeScript - Nicolas Charpentier
This component is a wrapper around @reach/router's Link component that adds useful enhancements specific to Gatsby. All props are passed through ...
Read more >
Using Gatsby Link with TypeScript - Stack Overflow
Show activity on this post. I'm using Gatsby Link with TypeScript and I want to pass parameters to linked component from source component....
Read more >
Linking Between Pages | Gatsby
This guide covers how to link between pages in a Gatsby site. The Gatsby link component. The Gatsby <Link /> component is for...
Read more >
gatsby-link - npm
Start using gatsby-link in your project by running `npm i gatsby-link`. ... TypeScript icon, indicating that this package has built-in type ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found