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

chore(gatsby): Migrate utils/page-data to TypeScript #23436

Closed
wants to merge 1 commit into from
Closed

chore(gatsby): Migrate utils/page-data to TypeScript #23436

wants to merge 1 commit into from

Conversation

hiwelo
Copy link
Contributor

@hiwelo hiwelo commented Apr 23, 2020

Description

This commit migrates the existing src/utils/page-data.ts file to TypeScript by trying to stick as close as possible to the original JavaScript file.

To take advantage of existing Redux typings, I tried to extend the typings from the IGatsbyPage interface describing page data.

Two typings are open to debate, the type returned by the promise line 24, and the typing for the result argument of the write function.

(also, first PR so I tried to stick to the contributing guidelines as much as possible, but let me know if something's off 😅)

Related Issues

Related to #21995

@hiwelo hiwelo requested a review from a team as a code owner April 23, 2020 20:34
export const write = async (
{ publicDir }: IPageDataContext,
{ componentChunkName, matchPath, path }: IGatsbyPage,
result: any
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, I was not really sure what is the shape of the result argument so I judge any being safer. But feel free to indicate a more precise typing for this line, if existing.

Comment on lines +71 to +79
export default {
fixedPagePath,
read,
remove,
write,
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a redundancy in the file exports to ensure the compatibility of the import statements in existing JavaScript files using the default statement in a require statement.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this and just update all the references to this file!

@hiwelo hiwelo marked this pull request as draft April 23, 2020 21:24
@hiwelo hiwelo marked this pull request as ready for review April 23, 2020 21:33
This commit migrates the existing `src/utils/page-data.ts` file to
TypeScript by trying to stick as close as possible to the original
JavaScript file.

To take advantage of existing Redux typings, I tried to extend the
typings from the `IGatsbyPage` interface describing page data.
@hiwelo hiwelo requested a review from blainekasten April 27, 2020 17:50
@hiwelo hiwelo added the status: needs core review Currently awaiting review from Core team member label May 5, 2020
interface IPageData
extends Pick<IGatsbyPage, "componentChunkName" | "matchPath" | "path"> {
result: any
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit of a nit. But I find these utility generics hard to understand. Can we change this to this?

interface IPageData {
  componentChunkName: IGatsbyPage["componentChunkName"]
  matchPath: IGatsbyPage["matchPath"]
  path: IGatsbyPage["path"]
  result: any // also, can this be improved?
} 

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think any might be an instance of ExecutionResult from graphql. Try that out


interface IPageDataContext {
publicDir: string
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this isn't the scope of this, but can we actually change this to not wrap these in an object? All of the invocations that are like fn({ publicDir }) could and should just be fn(publicDir). So this will require updating the references too


export const write = async (
{ publicDir }: IPageDataContext,
{ componentChunkName, matchPath, path }: Exclude<IPageData, "result">,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this Exclude. We try to avoid complex typing. I think it would be easier to just have two interfaces. One that extends the other and adds result.

Copy link
Contributor

@blainekasten blainekasten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a wonderful start. Getting close!

@hiwelo
Copy link
Contributor Author

hiwelo commented May 11, 2020

Closing because of missing fork branch, can't push an update.
Opened #23991 instead, with all requested changes

@hiwelo hiwelo closed this May 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs core review Currently awaiting review from Core team member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants