Home > Net >  Typescript is compiling extremely slowly for a single file
Typescript is compiling extremely slowly for a single file

Time:10-12

I'm new to Typescript and am trying to follow along with a tutorial on Youtube. For some reason, running tsc myfile.ts takes an extremely long time to compile. I'm talking maybe 40 seconds or so. My file is very short too, it's just some simple Javascript to get us acquainted with defining types in Typescript. For the instructor, it takes a second for his file to get compiled, but I have to wait quite a while. How can I reduce the compile time?

I installed Typescript globally by running npm install -g typescript. My project doesn't have any .tsconfig files, it's literally just a .ts file and a basic index.html file.

I don't think it's relevant, but just in case, here's the .ts file I'm 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);

Also, some information that is maybe useful is I'm using a Macbook Pro with an M1 chip.

Thanks!

Edit: The file DOES eventually compile, the problem is it just takes an extraordinarily long amount of time to do so.

CodePudding user response:

If it's taking a long time to compile your project, it may be that the compiler is looking for a tsconfig.json and not able to find one. (Does it ever compile?)

My recommendation is to create a tsconfig.json file in the folder of your project with tsc --init.

This will create the tsconfig.json file with default settings. Also, see the CLI documentation for more details. Or type tsc --help in your command prompt/terminal.

CodePudding user response:

The reason of it is bcuz you have typescript install globally. Just because you don't have a tsconfig.json the it doesn't mean there isn't one your global typescript has one and it doesn't have an exclude or and a include property, there for you might be just compiling one file, but your typescript is checking all the files inside your global node_modules.

So either install typescript locally and add a tscofing to yow project or uninstall typescript globally and instead run yow code with npx.

To answer the question it run slow because your typescript is checking all them files at the global node_modules

  • Related