-
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
chore(gatsby): convert gatsby-link to typescript #37543
base: master
Are you sure you want to change the base?
Conversation
I do have a pesky ref type issue in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, thanks for the PR!
Our CI should be running against your PR now so you can also check that to see your progress.
I've left a bunch of review comments on conventions, code style etc.
Ideally this PR doesn't change any behavior and only converts every piece. For this we'll also need to check the generated type definition file and compare it against the manually written one.
@@ -1 +1,8 @@ | |||
{} | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file change is not necessary since we use microbundle
to generate the output.
You can check out https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-script to see how it's used (also has relevant package.json
bits)
beforeEach(() => { | ||
global.__BASE_PATH__ = `` | ||
global.__PATH_PREFIX__ = `` | ||
}) | ||
|
||
afterEach(cleanup) | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR has a lot of eslint-disable for the explicit function return type. We have that linting rule for a reason 😅
Please remove all instances of that eslint-disable and add the return types
global.__BASE_PATH__ = pathPrefix | ||
const source = createMemorySource(sourcePath) | ||
const history = createHistory(source) | ||
|
||
const utils = render( | ||
<LocationProvider history={history}> | ||
<Link | ||
{...linkProps} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was that change necessary?
@@ -386,7 +419,7 @@ describe(`ref forwarding`, () => { | |||
}) | |||
|
|||
it(`handles a RefObject (React >=16.4)`, () => { | |||
const ref = React.createRef(null) | |||
const ref = React.createRef() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was that change necessary?
@@ -1,6 +1,17 @@ | |||
import * as React from "react" | |||
import { NavigateFn, LinkProps } from "@reach/router" // These come from `@types/reach__router` | |||
|
|||
declare global { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The types should be autogenerated from the source now and the manual file should no longer exist
|
||
beforeEach(() => { | ||
global.__TRAILING_SLASH__ = `` | ||
global.__PATH_PREFIX__ = undefined | ||
}) | ||
|
||
const getRewriteLinkPath = (option = `legacy`, pathPrefix = undefined) => { | ||
const getRewriteLinkPath = ( | ||
option: string = `legacy`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of a string
this should be the TrailingSlash
type
@@ -110,7 +133,7 @@ class GatsbyLink extends React.Component { | |||
// If IO supported and element reference found, setup Observer functionality | |||
this.io = createIntersectionObserver(ref, inViewPort => { | |||
if (inViewPort) { | |||
this.abortPrefetch = this._prefetch() | |||
this.abortPrefetch = this._prefetch() as { abort: () => void } | null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type-casting is a code smell, it seems like abortPrefetch
is not properly typed then
this.abortPrefetch = null | ||
this.handleRef = this.handleRef.bind(this) | ||
class GatsbyLink extends React.Component<IGatsbyLinkProps, IGatsbyLinkState> { | ||
readonly state: IGatsbyLinkState = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two things here:
- Removing the constructor doesn't seem necessary, you can also type that one: https://fettblog.eu/typescript-react/components/#class-components
- According to https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/class_components/ the
readonly
isn't necessary
So I'd go with the constructor for now to keep the functional changes minimal in this PR (more straight-forward conversion)
@@ -1,23 +1,28 @@ | |||
import { resolve } from "@gatsbyjs/reach-router" | |||
// Specific import to treeshake Node.js stuff | |||
import { applyTrailingSlashOption } from "gatsby-page-utils/apply-trailing-slash-option" | |||
import { applyTrailingSlashOption, type TrailingSlash } from "gatsby-page-utils" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The import from gatsby-page-utils/apply-trailing-slash-option
was on purpose for better tree-shaking. So if you need the type from the root, add an additional import instead:
import type { TrailingSlash } from "gatsby-page-utils"
@@ -0,0 +1,3 @@ | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-use what gatsby-script
has:
{
"extends": "../../tsconfig.json",
"exclude": ["node_modules", "src/__tests__", "dist"],
"compilerOptions": {
"declaration": false
}
}
Sorry I got busy irl and kinda forgot about this PR, I'll address all the feedback these days! |
Description
Convert gatsby-link to TS.
Documentation
Related Issues
The previous PR was written 3 years ago and closed more than 6 months ago, as suggested in the closing message, I took the liberty of redoing it and open a new PR #22027