Compiling a solo Typescript file is taking an unexpectedly long time

I recently started learning Typescript and encountered a slow compilation issue while following a tutorial on Youtube. When I run tsc myfile.ts, it takes about 40 seconds to compile, even though my file is short with just basic Javascript code for understanding type definitions in Typescript. Interestingly, the instructor's file compiles in a second, but I have to wait much longer. How can I speed up the compilation process?

To install Typescript globally, I used the command npm install -g typescript. My project does not contain any .tsconfig files; it only consists of a single .ts file and a simple index.html file.

It may not be relevant, but here is the content of the .ts file that I am compiling:

function add(num1: number, num2: number, showResult: boolean, phrase: string) {
    const result = num1 + num2;
    if (showResult) {
        console.log(phrase + result)
    } else {
        return result
    }
}

const number1 = 5;
const number2 = 2.5
const printResult = true;
const resultPhrase = 'Result is: '

const result = add(number1, number2, printResult, resultPhrase);

Additionally, I am using a Macbook Pro with an M1 chip which might provide some context.

Thank you!

Edit: The file eventually compiles, but the main issue remains the excessive time it takes to complete the process.

Answer №1

The reason for the slow performance is due to having TypeScript installed globally. Even if you do not have a tsconfig.json, your global installation of TypeScript still has one and lacks exclude or include properties. This means that when compiling, TypeScript is checking all files within your global node_modules directory.

To resolve this issue, you can either install TypeScript locally and add a tsconfig to your project, or uninstall TypeScript globally and instead use npx to run your code.

In summary, the slowdown occurs because TypeScript is scanning through all files in the global node_modules directory.

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

Is it possible to verify type property names using a union type?

Given type UnionType = 'prop1' | 'prop2' | 'prop3'; type DerivedType = { prop1: string; prop2: number; prop3: boolean; }; Is there a method to define DerivedType in such a way that if I introduce a new member to UnionT ...

Tips for converting numerical values in a JSON object to strings within a TypeScript interface

{ "id": 13, "name": "horst", } in order to interface A { id: string; name: string; } When converting JSON data of type A to an object, I expected the conversion of id from number to string to happen automatically. However, it doesn' ...

Using TypeScript path aliases to resolve import errors

After creating a Vue.js project using Vue CLI 3 and setting up the import statement with the @ alias, I encountered an error when running npm run build. Can you explain why this happened? Error Message $ npm run build > [email protected] build / ...

Understanding app.bundle.js errors in Ionic 2

I recently updated an app from an earlier Ionic 2 Alpha version to the latest Ionic 2 Beta 10. This upgrade has caused some of my code to break, and I'm struggling to understand the errors shown in the JavaScript console. The TypeScript is all bundled ...

Is there a method (hack) to instantiate an instance of class `T` within a generic class?

Is there a way or workaround to instantiate an object of type T within a generic class? type Foo = { /*...*/ }; class Bar < T extends Foo > { public readonly foo: T; public constructor( init?: { foo: T } | undefined ) { this.f ...

Asserting types for promises with more than one possible return value

Struggling with type assertions when dealing with multiple promise return types? Check out this simplified code snippet: interface SimpleResponseType { key1: string }; interface SimpleResponseType2 { property1: string property2: number }; inter ...

Utilize a third-party module's repository within my module in NestJS

Currently, I am working on developing an application using Nestjs and have created two modules: User and Auth with the following structure: https://i.sstatic.net/N8Zyz.png To interact with the User entity, I needed to inject the UsersService into the Aut ...

Create a new object containing a series of function expressions, but exclude the first function parameter

In my current setup, I have a variable called storePattern const storePattern = { state: { }, mutations: { }, actions: {}, modules: { modal: { actions: { openModal(store, name: string): boolean { console.log('Op ...

Did the IBM MobileFirst client miss the call to handleFailure?

I am currently utilizing the IBM MFP Web SDK along with the provided code snippet to send challenges and manage responses from the IBM MobileFirst server. Everything functions properly when the server is up and running. However, I have encountered an iss ...

Leverage the power of InfiniteSWR in your project by integrating it seamlessly with

If you're looking for a comprehensive example of how to integrate useSWR hooks with axios and typescript, check out the official repository here. import useSWR, { SWRConfiguration, SWRResponse } from 'swr' import axios, { AxiosRequestConfig, ...

delayed updating of property not visible in angular 10 immediately

I needed to hide a div based on a condition, so I decided to use the hidden property like below: <div [hidden]="isControlDisplayed()?false:true"> The isControlDisplayed() method determines whether to show or hide the div based on the value ...

typescript: declaring types in a separate JavaScript file

Imagine you have a JavaScript library that exports some types for use (let's call it js1.js). You also have some TypeScript code sitting in a <script type="module"> tag that you want to use these types with (let's say ts1.ts). To make this ...

Is it possible to retrieve the signature for every method within a class?

Let's consider a scenario where we have a class defined as follows: class Actions { static FooAction = 'foo' as const; someAction1() { return { type: Actions.FooAction, payload: {a: 1, b:2} }} static BarAction = &apos ...

Error message displayed indicating script not found after executing yarn tsc within a Docker container

Using docker, I successfully built my project by running yarn tsc. However, when I executed docker run -p88:5011 accounts2 I encountered the following error: PM2 error: Error: Script not found: /home/accounts/dist/server/index.js. This error occurs despit ...

Construct a string by combining the elements of a multi-dimensional array of children, organized into grouped

My task involves manipulating a complex, deeply nested array of nodes to create a specific query string structure. The desired format for the query string is as follows: (FULL_NAME="x" AND NOT(AGE="30" OR AGE="40" AND (ADDRESS ...

Utilizing the functionalities provided by node.js, I came across an issue and sought out a solution for the error message "TypeError: __WEBPACK_IMPORTED_MODULE_3_fs__.writeFile is not a function"

I have created a project centered around {typescript, react, electron, gaea-editor}. During an event, I utilized fs.writeFile() but encountered an error. The specific error message was: TypeError: __WEBPACK_IMPORTED_MODULE_3_fs__.writeFile is not a functi ...

Why isn't my event handler triggering when working with TypeScript services and observables?

I am currently working on a project in Angular 2 where I am incorporating observables and services in typescript. However, I have encountered an issue where the event handler in my observable is not being triggered. Below is the code snippet: The Service ...

Why is it that when the form is submitted, the value becomes unclear?

This is a form with HTML and TypeScript code that I am working on. <form> <div class="form-group"> <input class="form-control" ([ngModel])="login" placeholder="Login" required> </div> <div class="form-group"> &l ...

Failure to Execute Angular HttpClient Request

I'm facing an issue with firing the HttpClient request. It seems like there might be a problem with importing or providing, but I can't pinpoint where it is exactly. The API works fine, but the call never goes through. Here are the environment/v ...

data not corresponding to interface

I am encountering an issue that says Type '({ name: string; href: string; icon: IconDefinition; } | { name: string; href: string; icon: IconDefinition; childs: { name: string; href: string; icon: IconDefinition; }[]; })[]' is missing the followin ...