-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
GatsbyLinkProps typescript type not working for custom <Link /> component #16682
Comments
I reproduced the issue. The problem seems to be with the "ref" property. Gatsby Link is using @reach/router's link internally. Gatsby Link is exported with forwardRef, which gives you access to the ref as a property, and it forwards it to @reach/router's Link. A workaround I came up with is to just exclude the "ref" property from GatsbyLinkProps. It is not ideal, but you can still use "innerRef", which will properly be passed to @reach/router to handle. This is how you would make your custom component:
Keep in mind that this is just a workaround until the issue can be addressed properly. |
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 30 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. You can also add the label "not stale" to keep this issue open! 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! 💪💜 |
I'm also having that issue. I was trying to infer type of Link's props but it doesn't work either
|
I could make a PR on Gatsby typings, what do you think? |
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here. Thanks for being a part of the Gatsby community! 💪💜 |
Just for future developers searching for this issue, I implemented this way: In Typescript version 4, you can't use {} as type, you have to use Record<string, unknown> |
Closing this as stale since in the meantime Gatsby v3 and updated related packages were released. Please try with the latest versions and if you still see this problem open a new bug report (it must include a minimal reproduction). |
no need to omit or override ref. just intersect GatsbyLinkProps with HTMLAnchorElement. here is how you can wrap Gatsby Link using TypeScript: import { Link as GatsbyLink } from 'gatsby';
import type { GatsbyLinkProps } from 'gatsby';
function Link(
props: GatsbyLinkProps<Record<string, unknown>> & HTMLAnchorElement
) {
return <GatsbyLink {...props} />;
}
export default Link; |
Description
I'm trying to extend a
<Link />
component in my Typescript project. This is a simplified example:I'm getting the error:
However,
GatsbyLinkProps
seems to be what the<Link />
component defines as its prop type, so this should work. Am I doing something wrong, or is this a bug with the type definitions?Steps to reproduce
Go to https://codesandbox.io/s/gatsby-typescript-link-frt1h and open src/pages/index.tsx, or
gatsby-plugin-typescript
to the projectExpected result
No compile error
Actual result
Compile error as described above
Environment
https://codesandbox.io/s/gatsby-typescript-link-frt1h
The text was updated successfully, but these errors were encountered: