Gatsby TypeScript Plugin

5.16.0 · active · verified Sun Apr 19

gatsby-plugin-typescript allows Gatsby to process and build TypeScript and TSX files, integrating TypeScript transpilation into the Gatsby build pipeline using `@babel/preset-typescript`. The plugin's current stable version, 5.16.0, is designed for Gatsby v5, and its releases typically align with major and minor Gatsby core updates. A key differentiator is that this plugin focuses solely on transpilation, meaning it transforms TypeScript code into JavaScript without performing type checking itself. Developers are expected to handle type checking separately, often through their IDE or a dedicated `type-check` script. While it supports most common TypeScript features, it has specific limitations due to its Babel-based approach, such as not supporting namespaces, `const` enums, `export =`/`import =` syntax, or direct `baseUrl` configuration. The plugin is automatically included in Gatsby projects, requiring explicit configuration only for custom options.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart shows how to configure `gatsby-plugin-typescript` in `gatsby-config.js` and create a basic Gatsby page component using TypeScript and TSX syntax.

// gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: `gatsby-plugin-typescript`,
      options: {
        isTSX: true, // Enable TSX support
        allExtensions: true // Process all extensions including .ts and .tsx
      }
    }
  ]
};

// src/pages/index.tsx
import * as React from 'react';
import type { PageProps } from 'gatsby';

interface IndexPageProps extends PageProps {
  // Add any custom props if using page queries
}

const IndexPage: React.FC<IndexPageProps> = () => {
  const greeting: string = "Hello, Gatsby with TypeScript!";

  return (
    <main style={{ fontFamily: 'sans-serif', padding: 20 }}>
      <h1>{greeting}</h1>
      <p>This page is built with TypeScript and rendered by Gatsby.</p>
      <button onClick={() => alert('TypeScript in action!')}>
        Click Me
      </button>
    </main>
  );
};

export default IndexPage;

view raw JSON →