How to set up GatsbyJS with TypeScript
First things
Install TypeScript as dependency:
- npm:
|
|
- yarn
|
|
After installing, you should initialize TypeScript project by generating tsconfig.json file
|
|
Because in GatsbyJS you are using React components that includes JSX you should set specific parameter in tsconfig.json:
|
|
If you want to use TypeScript for your src/ files you can easily just rename all files in src/ from .js to .ts or .tsx. Gatsby support TypeScript out of the box.
TypeScript for gatsby-* files
If you want to write files gatsby-browser.js, gatsby-node.js, and others using TypeScript you should install and setup plugin gatsby-plugin-ts-config
Steps:
Installation:
- npm
npm install -D gatsby-plugin-ts-config
- yarn
|
|
Add gatsby-plugin-ts-config into gatsby-config.js
|
|
Using types in gatsby-node.ts
|
|