You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To see all of the types available and their generics look at our [TypeScript definition file](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/index.d.ts).
13
15
14
-
## `PageProps`
16
+
## Initializing a new project with TypeScript
17
+
18
+
You can get started with TypeScript and Gatsby by using the CLI:
19
+
20
+
```
21
+
npm init gatsby
22
+
```
23
+
24
+
In the prompts, select TypeScript as your preferred language. You can also pass a `ts` flag to the above command to skip that question and use TypeScript:
25
+
26
+
```
27
+
npm init gatsby -ts
28
+
```
29
+
30
+
## Usage in Gatsby
31
+
32
+
### `PageProps`
15
33
16
34
```tsx:title=src/pages/index.tsx
17
35
import*asReactfrom"react"
18
-
import { PageProps } from"gatsby"
36
+
importtype{ PageProps } from"gatsby"
19
37
20
38
const IndexRoute = ({ path }:PageProps) => {
21
39
return (
@@ -65,15 +83,19 @@ export const query = graphql`
65
83
`
66
84
```
67
85
68
-
## `gatsby-browser.tsx` / `gatsby-ssr.tsx`
86
+
### `gatsby-browser.tsx` / `gatsby-ssr.tsx`
87
+
88
+
> Support added in `gatsby@4.8.0`
69
89
70
90
You can also write `gatsby-browser` and `gatsby-ssr` in TypeScript. You have the types `GatsbyBrowser` and `GatsbySSR` available to type your API functions. Here are two examples:
71
91
72
92
```tsx:title=gatsby-browser.tsx
73
93
import*asReactfrom"react"
74
-
import { GatsbyBrowser } from"gatsby"
94
+
importtype{ GatsbyBrowser } from"gatsby"
75
95
76
-
exportconst wrapPageElement:GatsbyBrowser["wrapPageElement"] = ({ element }) => {
You can use `GetServerData`, `GetServerDataProps`, and `GetServerDataReturn` for [`getServerData`](/docs/reference/rendering-options/server-side-rendering/).
You can import the type `GatsbyConfig` to type your config object. **Please note:** There are currently no type hints for `plugins` and you'll need to check the [current limitations](#current-limitations) and see if they apply to your `gatsby-config` file.
163
+
164
+
```ts:title=gatsby-config.ts
165
+
importtype { GatsbyConfig } from"gatsby"
166
+
167
+
const config:GatsbyConfig= {
168
+
siteMetadata: {
169
+
title: "Your Title",
170
+
},
171
+
plugins: [],
172
+
}
173
+
174
+
exportdefaultconfig
175
+
```
176
+
177
+
### `gatsby-node.ts`
178
+
179
+
> Support added in `gatsby@4.9.0`
180
+
181
+
You can import the type `GatsbyNode` to type your APIs by accessing keys on `GatsbyNode`, e.g. `GatsbyNode["sourceNodes"]`. **Please note:** You'll need to check the [current limitations](#current-limitations) and see if they apply to your `gatsby-node` file.
All the files mentioned above can also be written and used inside a [local plugin](/docs/creating-a-local-plugin/).
223
+
224
+
## `tsconfig.json`
225
+
226
+
Essentially, the `tsconfig.json` file is used in tools external to Gatsby e.g Testing Frameworks like Jest, Code editors and Linting libraries like EsLint to enable them handle TypeScript correctly. You can use the `tsconfig.json` from our [gatsby-minimal-starter-ts](https://github.com/gatsbyjs/gatsby/blob/master/starters/gatsby-starter-minimal-ts/tsconfig.json).
227
+
228
+
## Type Hinting in JS Files
229
+
230
+
You can still take advantage of type hinting in JavaScript files with [JSdoc](https://jsdoc.app/) by importing types directly from Gatsby. You need to use a text exitor that supports those type hints.
[vanilla-extract](https://vanilla-extract.style/) helps you write type‑safe, locally scoped classes, variables and themes. It's a great solution when it comes to styling in your TypeScript project. To use vanilla-extract, select it as your preferred styling solution when initializing your project with `npm init gatsby`. You can also manually setup your project through [gatsby-plugin-vanilla-extract](/plugins/gatsby-plugin-vanilla-extract/) or use the [vanilla-extract example](https://github.com/gatsbyjs/gatsby/tree/master/examples/using-vanilla-extract).
259
+
260
+
## Migrating to TypeScript
261
+
262
+
Gatsby natively supports JavaScript and TypeScript, you can change files from `.js`/`.jsx` to `.ts`/ `tsx` at any point to start adding types and gaining the benefits of a type system. But you'll need to do a bit more to be able use Typescipt in `gatsby-*` files:
263
+
264
+
- Run `gatsby clean` to remove any old artifacts
265
+
- Convert your `.js`/`.jsx` files to `.ts/.tsx`
266
+
- Install `@types/node`, `@types/react`, `@types/react-dom`, `typescript` as `devDependencies`
267
+
- Add a `tsconfig.json` file using `npx tsc init` or use the one from [gatsby-minimal-starter-ts](https://github.com/gatsbyjs/gatsby/blob/master/starters/gatsby-starter-minimal-ts/tsconfig.json)
268
+
- Rename `gatsby-*` files:
269
+
-`gatsby-node.js` to `gatsby-node.ts`
270
+
-`gatsby-config.js` to `gatsby-config.ts`
271
+
-`gatsby-browser.js` to `gatsby-browser.tsx`
272
+
-`gatsby-ssr.js` to `gatsby-ssr.tsx`
273
+
- Address any of the [current limitations](#current-limitations)
274
+
275
+
## Current limitations
276
+
277
+
There are some limitations currently that you need to be aware of. We'll do our best to mitigate them in our code or through contributions to upstream dependencies.
278
+
279
+
### Parcel TypeScript features
280
+
281
+
Parcel is used for the compilation and it currently has [limitations on TypeScript features](https://parceljs.org/languages/typescript/), namely:
282
+
283
+
- No support for `baseUrl` or `paths` inside `tsconfig.json`
284
+
- It implicitly enables the [`isolatedModules`](https://www.typescriptlang.org/tsconfig#isolatedModules) option by default
285
+
286
+
### `__dirname`
287
+
288
+
You can't use `__dirname` and `__filename` in your files. You'll need to replace these instances with a `path.resolve` call. Example diff for a `gatsby-config` file:
289
+
290
+
```diff
291
+
+ import path from "path"
292
+
293
+
const config = {
294
+
plugins: [
295
+
{
296
+
resolve: `gatsby-source-filesystem`,
297
+
options: {
298
+
name: `your-name`,
299
+
+ path: path.resolve(`some/folder`),
300
+
- path: `${__dirname}/some/folder`,
301
+
},
302
+
},
303
+
]
304
+
}
305
+
306
+
export default config
307
+
```
308
+
309
+
Progress on this is tracked in [Parcel #7611](https://github.com/parcel-bundler/parcel/issues/7611).
310
+
311
+
### `require.resolve`
312
+
313
+
You can't use `require.resolve` in your files. You'll need to replace these instances with a `path.resolve` call. Example diff for a `gatsby-node` file:
Progress on this is tracked in [Parcel #6925](https://github.com/parcel-bundler/parcel/issues/6925).
323
+
324
+
### Other
325
+
326
+
- Workspaces (e.g. Yarn) are not supported yet.
327
+
- When changing `siteMetadata` in `gatsby-config` no hot-reloading will occur during `gatsby develop`. A restart is needed at the moment.
328
+
329
+
## Other Resources
137
330
138
-
TypeScript integration is supported through automatically including [`gatsby-plugin-typescript`](/plugins/gatsby-plugin-typescript/). Visit that link to see configuration options and limitations of this setup.
331
+
TypeScript integration for pages is supported through automatically including [`gatsby-plugin-typescript`](/plugins/gatsby-plugin-typescript/). Visit that link to see configuration options and limitations of this setup.
139
332
140
333
If you are new to TypeScript, check out these other resources to learn more:
0 commit comments