Prevent Gatsby from compiling when encountering a TypeScript error

When working on my CRA TypeScript projects, I appreciate how TypeScript prevents the dev server from compiling whenever there's an error in my code. Instead, it displays the error in both the browser and console, which helps me quickly identify and fix issues.

However, in my new Gatsby project, this behavior is different. While my IDE alerts me about any errors, running gatsby develop ignores those errors and compiles the code anyway, similar to how it would with plain JavaScript. How can I configure Gatsby to mimic the behavior I am accustomed to from CRA?

I have integrated gatsby-plugin-typescript into my project setup. This is what my gatsby-config.js looks like:

module.exports = {
  siteMetadata: {
    title: `Gatsby Default Starter`,
    description: `Kick off your next, great Gatsby project with this default starter. This barebones starter ships with the main Gatsby configuration files you might need.`,
    author: `@gatsbyjs`,
  },
  plugins: [
    `gatsby-plugin-react-helmet`,
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `images`,
        path: `${__dirname}/src/assets`,
      },
    },
    `gatsby-transformer-sharp`,
    `gatsby-plugin-sharp`,
    {
      resolve: `gatsby-plugin-manifest`,
      options: {
        name: `gatsby-starter-default`,
        start_url: `/`,
      },
    },
    {
      resolve: `gatsby-plugin-typescript`,
      options: {
        isTSX: true, // defaults to false
        allExtensions: true, // defaults to false
      },
    },
    `gatsby-plugin-emotion`,
    // this (optional) plugin enables Progressive Web App + Offline functionality
    // To learn more, visit: https://gatsby.dev/offline
    // `gatsby-plugin-offline`,
  ],
}

This is my tsconfig.json file:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "downlevelIteration": true,
    "jsx": "react"
  },
  "include": [
    "src/**/*"
  ]
}

Answer №1

As far as I know, achieving this cannot be done using gatsby-plugin-typescript.

According to the Gatsby documentation:

This plugin relies on babel-plugin-transform-typescript for transpiling TypeScript code and does not include type checking.

If you wish to replicate the behavior seen in CRA, consider using gatsby-plugin-ts instead. Keep in mind that unlike gatsby-plugin-typescript, this one is considered "unofficial". I stumbled upon it while searching through Gatsby starters and found this one, which works great.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble inserting the current time into Firebase Firestore

Currently, I am looking to use the current time as an input in Firebase Firestore (timestamp). Initially, when using the code snippet below: today: number = Date.now(); everything appeared to function correctly. However, the time was only updated once, s ...

Verify web connectivity in an Angular2 (non-Ionic) Cordova application

Our team has developed an Angular2 Cordova application (not Ionic) that interacts with multiple backend services. We want the app to display a specific page (Component) if a user is offline. Although we have already created this feature, we are unsure of ...

Modifying an Angular Component Template following an API Response

As someone relatively new to Angular, I come with some experience from working with other JavaScript frameworks like Vue and React. Currently, I am developing an Angular Lab application that interacts with 2 APIs to retrieve information. After receiving th ...

Why is it so difficult for the TypeScript compiler to recognize that my variables are not undefined?

Here is the code snippet in question: function test(a: number | undefined, b: number | undefined) { if (!a && !b) { console.log('Neither are present'); return; } if (!b && !!a) { console.log('b is not present, we ...

Creating a ref with Typescript and styled-components: A comprehensive guide

Trying to include a ref into a React component looks like this: const Dashboard: React.FC = () => { const [headerHeight, setHeaderHeight] = useState(0); const headerRef = React.createRef<HTMLInputElement>(); useEffect(() => { // @ts ...

Is there a way to define type information for a global variable when utilizing dynamic import within a function?

Here is a simplified version of my server code: server.ts import google from "googleapis"; const androidPublisher = google.androidpublisher("v3"); app.use('something', function(req, res, n){ ... }) ...(only one of the dozens of other meth ...

The class instances are not invoking the decorators

I'm experiencing issues with my decorators. It seems that the decorators are not being invoked on every instance of the class. While I understand that decorators are called during declaration time, I am wondering if there is a way to call them for eac ...

Retrieving information from an array using a variable results in undefined, but using a specific number returns the desired object

Currently, in the process of learning TypeScript as my second language, I am encountering some challenges with arrays. I have observed peculiar behavior when accessing the list with a variable as opposed to a hardcoded number. The code snippet in questi ...

What is the best way to distribute a Typescript module across multiple projects without having to duplicate it each time?

I currently have two folders named shared and project1 within a larger folder called node. I am in the process of working on multiple Node projects that are independent and are all located within the node folder. Within the shared folder, there is an inde ...

Customize the text displayed in a dropdown menu in Angular Material based on the selection made

I am working with a multi-select dropdown menu that includes an option labeled "ALL" which, when selected, chooses all available options in the list. My goal is to display "ALL" in the view when this option is chosen or when the user manually selects all t ...

Utilize Sequelize's cascade feature within your application

I'm currently building a web application using sequelize and typescript. In my database, I have three tables: WfProjectObject, which is connected to WfProject, and WfStep, also linked to WfProject. My goal is to include WfStep when querying WfProject ...

Issue with Angular: Unable to bind to 'ngForFrom' as it is not recognized as a valid property of 'a'

I seem to be encountering a recurring error that I can't seem to resolve, despite trying various solutions. Below is the content of my appModule.ts file: import { CommonModule } from '@angular/common'; import { NgModule } from '@angula ...

Implement dynamic typing in the sort function to restrict it to only accept number properties

I need help creating a pipe that can sort an array of objects based on a specified property. While I have managed to make it work, I am encountering a type error in my code. Here is the snippet: export const sortByProperty = <T>(a: T, b: T, property: ...

The user's type from express-session is not being properly detected by Typescript

I have a process where I retrieve the user object from the database and set it on the express-session: export const postLogin = async ( request: Request, response: Response, next: NextFunction ): Promise<void> => { try { re ...

Is it better to import from a <variable> rather than a hardcoded string in TypeScript?

https://www.example.com/typescript-dynamic-import Can Typescript handle dynamically setting the import path into a variable? For example, can we transform this: import {HomeComponent} from './dashboard/home/home.component'; Into something lik ...

Is it feasible to access a service instance within a parameter decorator in nest.js?

I am looking to replicate the functionality of Spring framework in nest.js with a similar code snippet like this: @Controller('/test') class TestController { @Get() get(@Principal() principal: Principal) { } } After spending countless ho ...

Breaking down a MUI v5 theme into individual files based on theme options - a step-by-step guide

Currently, I am working on a significant MUI v5 theme that spans around 800 lines, including TypeScript type augmentation. The structure looks something like this: let myTheme = createTheme({ palette: { primary: { // lots of custom props }, ...

Dealing with Typescript type errors when using Ramda Transducers can be challenging, but it's important to know how to handle these confusing type

Upon reviewing the Ramda documentation regarding transduce, it was noted that two examples were provided, both resulting in different errors being thrown by the Typescript compiler. Example 1: test('ex. 1', () => { const numbers = [1, 2, 3 ...

Tips for utilizing express in your typescript projects

I'm curious about the transition of definition files from tsd to typings, and now to @types. How can I incorporate @types in a node/express project? What is currently preferred and what's the reason for moving from tsd to typing and finally to @t ...

Update and insert fresh information into the Angular file

I currently have a CSV file stored in my assets folder. The file path is 'assets/data.csv' Here is how I am able to read the contents of the file: readLocalCSVFile() { this._http.get('assets/data.csv', { responseType: 'text ...