Upgrade Your Coding Skills: Learn How to Install TypeScript in Minutes

First, make sure you have Node.js and npm (which comes with Node) installed. You can check by running the following commands in your terminal:

node -v
npm -v

If you don’t have Node.js installed, you can download it from the official website (https://nodejs.org/) and follow the installation instructions for your operating system.

Once you have Node.js and npm set up, you can install TypeScript globally on your machine by running the following command:

npm install -g typescript

The -g flag tells npm to install the package globally, so you can use the TypeScript compiler (tsc) from anywhere on your machine.

Next, let’s create a new directory for your project, and inside that directory, create a file called tsconfig.json. This file will contain the configuration options for the TypeScript compiler.

Here’s a basic tsconfig.json file that you can use as a starting point:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "outDir": "./dist",
        "rootDir": "./src",
        "strict": true,
        "esModuleInterop": true
    },
    "include": [
        "src/**/*"
    ]
}

This configuration sets the target language version to ES5, the module system to CommonJS, and the output directory to ./dist. It also enables strict type checking and enables interoperability between CommonJS and ES modules.

With this setup, you can put your TypeScript code under src directory and the transpiled JavaScript code will be placed in dist directory.

To compile your TypeScript code, you can run the following command:

tsc

This will use the tsconfig.json file in the current directory to configure the TypeScript compiler.

You can also use a script in your package.json

"scripts": {
    "build": "tsc"
  },

and run it using

npm run build

And that’s it! You’re now ready to write some, sweet TypeScript code. Happy coding!