-
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): add TypeScript definitions for config & plugin APIs #10897
Conversation
I created TypeScript definitions for the [`gatsby-config.js`](https://www.gatsbyjs.org/docs/gatsby-config/#gatsby-config), [`gatsby-node.js`](https://www.gatsbyjs.org/docs/node-apis/), [`gatsby-browser.js`](https://www.gatsbyjs.org/docs/browser-apis/), and [`gatsby-ssr.js`](https://www.gatsbyjs.org/docs/ssr-apis/) APIs. Some of the definitions could use more details (for example, many functions are simply defined as `Function`), but at least every method, parameter, and property are defined, which provides good intellisense and code-completion assistance in tools like VSCode. 😎
That's really cool! Users would need to annotate types of the exports in gatsby-X files, right? How does it look like? I was working on something similar (but using jsdoc) and I did hit lot of problems but made it to somewhat work: Reason for using jsdoc instead of TS definition was that we generate API reference ( https://www.gatsbyjs.org/docs/node-apis/ ) from that, so we wouldn't need to maintain definitions in multiple places (typescript defs could not always be up to date) |
@pieh - Sorry, I should have included a usage example in my original description. VSCode (and other TypeScript-aware editors) support TypeScript definitions in plain JavaScript files (like For example, here's what my // @ts-check
const gatsby = require("gatsby");
/**
* @type {gatsby.GatsbyConfig}
*/
const config = {
siteMetadata: {
title: "Welcome to my website!",
},
plugins: [
"gatsby-plugin-typescript",
{
resolve: "gatsby-source-filesystem",
options: {
name: "docs",
path: `${__dirname}/src/docs/`,
},
},
],
};
module.exports = config; Because I've declared my |
thank you for this 👍 |
Oh wait! Before you merge this! I hadn't heard any feedback on this PR, so I thought it wasn't going to be merged. So I was preparing to submit type definitions to DefinitelyTyped instead. But it's always preferrable to have type definitions in the library package itself, so I'm glad to see that you're interested. Here are the latest type definitions (attched ZIP file), which I was preparing to submit to DefinitelyTyped. I've done a lot of cleanup, including separating the definitions into multiple files and correcting a few mistakes in my original definitions. In addition, I've added types for some Gatsby plugins too.
|
Hi @JamesMessinger 👋 |
This is great stuff! Would love to have it merged! |
I've tried to merge in the zip changes, and add my own API reference JSDoc comments based on this PR in #13619 |
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.
I'm updating the interfaces as there were some issues when trying to use it. (give me a sec to wrap up ^^)
for example this wasn't working:
/** @typedef {import('gatsby').GatsbyNode} GatsbyNode */
/** @type {GatsbyNode} */
const typedExports = exports
typedExports.createPages = (args, plugins) => { }
I might be just missing a lot of ts knowledge.
@orta maybe you know how to solve this. Typings are correct as you can see below: There is a problem as typescript can't detect the types of the params as it's a union of functions. So args & plugins are treat as an any value (if I remove the union and put only 1 function inside the d.ts file it works) |
sweet let me rollback my changes. |
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.
Sweet, thanks for doing the hard work of creating types for our apis so much appreciated!
Holy buckets, @JamesMessinger — we just merged your PR to Gatsby! 💪💜 Gatsby is built by awesome people like you. Let us say “thanks” in two ways:
If there’s anything we can do to help, please don’t hesitate to reach out to us: tweet at @gatsbyjs and we’ll come a-runnin’. Thanks again! |
published in gatsby@2.3.34 |
Description
I added TypeScript definitions for the
gatsby-config.js
,gatsby-node.js
,gatsby-browser.js
, andgatsby-ssr.js
APIs.Some of the definitions could use more details (for example, many functions are simply defined as
Function
), but at least every method, parameter, and property are defined, which provides good intellisense and code-completion assistance in tools like VSCode. 😎Related Issues
N/A