-
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
Is it possible to write the gatsby config in TypeScript? #1457
Comments
It'd be cool to add support for this. Perhaps the existing Typescript plugin can just add |
That would be a good solution. |
Edit: This comment was written years ago when Typescript was much less popular. Most of the framework code is now written in Typescript. We're very open to community PRs adding better support for Typescript to core files. Closing this as probably won't ever support it in core. |
Edit: Before you attempt the solution below, please update to 4.9 (https://www.gatsbyjs.com/docs/reference/release-notes/v4.9/#support-for-typescript-in-gatsby-config-and-gatsby-node) and use the built-in way of using TypeScript files. Quick note for anyone looking to do this - it's very straightforward. Just add require('source-map-support').install()
require('ts-node').register({
compilerOptions: {
module: 'commonjs',
target: 'es2017',
},
})
// typescript files
exports.createPages = require('./lib/createPages')
exports.onCreateNode = require('./lib/onCreateNode') There aren't any TS typings for the gatsby node API, but it's pretty easy to create some to cover your own app's surface area and provide some safety. Full example: https://gist.github.com/clarkdave/53cc050fa58d9a70418f8a76982dd6c8#file-types-ts |
Small follow-up to @clarkdave's most helpful comment; ts-node automagically loads tsconfig.json, so if you've already got one in your project, you needn't import it when you register ts-node. You should be able to just call |
So, since the above, I've been trying to start a fresh project utilizing this approach. I'm now having newfound problems. Whenever
Will update when I find a solution, but wanted to leave this here in case anyone else was having the same trouble & pulling their hair out. The createPages.ts file I'm using definitely doesn't have any functions with a parameter called Full error:
It does appear that the un-typed parameter of |
🤦♂️ It was as simple as this: I was calling Once I simply made it require ts-node once in gatsby-config.js, it worked. |
Pardon the spam... trying to decide the most appropriate place to leave this comment. If you've read this far looking for answers to the above problem, just skip this note! Everything you need to know is above. :) I've been toying around with gatsby themes. I created a theme, and installed it into my project via yarn (that is, as a dependency in my package.json). My theme used the above solution for pulling typescript files into
The typescript file I was importing had an import statement at the top. That's fine for typescript, not so fine for a vanilla node JS loader. I'm not sure why registering ts-node doesn't help in this case. Only workaround I can think of is to compile my typescript files into JS for redistribution. |
Using [this comment](gatsbyjs/gatsby#1457 (comment)) to get TS wired up, so we could import our TS Router in gatsby-node.
* SSR guides pages using router.pages Using [this comment](gatsbyjs/gatsby#1457 (comment)) to get TS wired up, so we could import our TS Router in gatsby-node. * Dont SSR dynamic pages
I found a perfect (almost) solution: use gatsby-plugin-typegen to generate types from GraphQL schema so that there will be:
In both gatsby-node configs and tsx components. Example: mdluo/blog-gatsby@68eb269 |
@mdluo I really like the solution you have come up with but it only seems to generate type definitions when using the Solved: got my files outside of |
@clarkdave 's solution is awesome. I actually took it a bit further and converted all Gatsby api files to TypeScript. There are multiple ways to achieve that. Check out my blogpost: Converting Gatsby Config and Node API to TypeScript |
I was able to fix the issue raised by @jonnybot0 regarding the unexpected token in an import statement by moving where the I had this error as a result of an import into
Calling |
FWIW I achieved this by moving any Gatsby config files, that I wanted written in TypeScript, to my The
"compilerOptions": {
"target": "ES2018", /* or at least ES2015 */
"module": "ESNext", /* or at least ES2015 */
"lib": ["dom"], /* <-- required! */
"jsx": "preserve", /* <-- required! */
"moduleResolution": "node", /* <-- required! */
/* other options... */
} My full
If you see the Here's an example of that: createPage({
path: `/${node.node_locale.toLowerCase()}/${BLOG_PATH}/${node.slug}`,
component: nodePath.resolve('./src/templates/Article/Article.tsx'),
context: {
id: node.contentful_id,
locale: node.node_locale,
},
})
/**
* DO NOT EDIT THIS FILE DIRECTLY
* Edit the source file at `./src/gatsby/gatsby-config.ts`
*/
module.exports = require('./.gatsby/src/gatsby/gatsby-config') I do the same for
Hope this helps some people. Let me know if it can be improved. |
This gist by @JohnAlbin worked great as of the current Gatsby version, and was very easy to follow and set up. No changes were required in the |
If you want I made a TypeScript starter (including Gatsby node hooks and config): |
Unfortunately some of the solutions to have TypeScript config files break with Gatsby v3 including I would love to see Gatsby having full fledged TypeScript support, also for the config files. I guess the best would be if it's somehow integrated into |
Totally supporting @openscript on having core-supported |
Come on! |
It's really embarrassing that Gatsby's Typescript support is half-assed like this. Everything inside the |
This issue was closed years ago when Typescript was much less popular. We're very open to PRs adding better support for Typescript to core files. If you'd like to discuss how to help make that happen, please open a new issue and let's make it happen. |
I used this way. It works fine during
require('ts-node').register()
module.exports = require('./gatsby-config.ts') project files:
|
@qhj The error you faced is due to this gatsby/packages/gatsby/src/utils/webpack.config.js Lines 798 to 813 in 3651e93
gatsby tries to cache |
@alvis I get it. Thanks for your reply. |
This is an unfortunate hardcoding which conflicts with the ts-node hack me and many use. What is not mentioned on this issue is that it's not really the core files which is interesting to get typescript support for; it's plugins and other code in the build pipeline which use Gatsby APIs. They can grow quite complex and need defs to be maintainable. |
For those using this setup running into this issue, I kept my module.exports = require('./gatsby-node.ts') and this seems to have gotten rid of the warning. Although now I get
|
By the way, I wonder why there is |
For anyone wondering how they could use the /** @type {import("gatsby").GatsbyConfig} */ |
Please go to #34613 and you can use TypeScript with Gatsby. By the time you're reading this it's also possible that the feature already was shipped in Gatsby 4. If that is the case the RFC will be marked as done. |
Future googlers: This is available in 4.9. https://www.gatsbyjs.com/docs/reference/release-notes/v4.9/#support-for-typescript-in-gatsby-config-and-gatsby-node |
Assuming I have ts-node, it would be nice to write the config file in TypeScript.
The text was updated successfully, but these errors were encountered: