Mapagam
  • JavaScript 
  • Web APIs 
  • TypeScript 
  • React 

How to Install and Set Up TypeScript in Minutes

Posted on March 29, 2025 • 6 min read • 1,080 words
Share via
Mapagam
Link copied to clipboard

Learn how to install and set up TypeScript in minutes with this easy guide. Get started with TypeScript in your project today!

On this page
1. What is TypeScript? 1.1 Why Use TypeScript? 2. Prerequisites 2.1 Node.js and npm 2.2 Text Editor or IDE 3. Installing TypeScript 3.1 Installing TypeScript Globally 3.2 Installing TypeScript Locally 4. Setting Up TypeScript in Your Project 4.1 Initialize a New Project 4.2 Create a TypeScript Configuration File 4.3 Create TypeScript Files 4.4 Compile TypeScript to JavaScript 4.5 Running the JavaScript Code 5. Automating the Build Process 5.1 Adding a Build Script 5.2 Watching Files for Changes 6. Conclusion

TypeScript has become one of the most popular programming languages for building robust, scalable, and maintainable web applications. As a superset of JavaScript, TypeScript provides static typing, interfaces, and other advanced features that help developers avoid common errors and build clean, efficient code.

1. What is TypeScript?

TypeScript is an open-source language developed and maintained by Microsoft. It builds on top of JavaScript by adding static types. Static typing is the ability to define types for variables, function parameters, and return values before the code is executed, which can help catch errors at compile time rather than runtime. It is also fully compatible with existing JavaScript code.

1.1 Why Use TypeScript?

  • Static Typing: TypeScript’s primary feature is its static typing system, which helps developers detect errors early.
  • Better Code Readability and Maintainability: With its strong typing, TypeScript makes it easier for teams to collaborate on large projects, reducing the risk of errors due to misunderstandings or ambiguous code.
  • Tooling and IDE Support: TypeScript is supported by many popular IDEs and code editors, such as Visual Studio Code, that provide features like autocompletion, code suggestions, and type checking.
  • Seamless JavaScript Integration: TypeScript code is compiled down to JavaScript, making it easy to integrate with existing JavaScript libraries or frameworks.

With the basics covered, let’s dive into how you can install and set up TypeScript quickly.

2. Prerequisites

Before you begin, make sure you have the following installed:

2.1 Node.js and npm

TypeScript is a tool that requires Node.js and npm (Node Package Manager). To check if Node.js and npm are installed on your system, run the following commands in your terminal:

node -v
npm -v

If you don’t have them installed, download the latest version of Node.js from the official Node.js website. This will also install npm automatically.

2.2 Text Editor or IDE

While TypeScript can be written in any text editor, it’s highly recommended to use an IDE or code editor with TypeScript support. Visual Studio Code is an excellent choice, as it provides built-in TypeScript support, which includes features like autocompletion and error highlighting.

3. Installing TypeScript

Once you have the necessary tools in place, installing TypeScript is easy. There are two primary methods for installing TypeScript: globally or locally.

3.1 Installing TypeScript Globally

Installing TypeScript globally allows you to use it in any project on your system without needing to install it again for each individual project. To install TypeScript globally, open your terminal and run the following command:

npm install -g typescript

This will install TypeScript globally on your system, making the tsc command available from anywhere. After installation, you can verify that TypeScript was installed successfully by checking the version:

tsc -v

You should see the version number of the installed TypeScript compiler.

3.2 Installing TypeScript Locally

If you prefer to install TypeScript on a per-project basis (which is often recommended for keeping dependencies manageable), navigate to your project folder and run the following command:

npm install --save-dev typescript

This installs TypeScript as a development dependency and adds it to your package.json file under the devDependencies section. You can then run the TypeScript compiler using an npm script, which is useful when working with a team to ensure everyone is using the same version of TypeScript.

4. Setting Up TypeScript in Your Project

Once you have TypeScript installed, it’s time to set it up in your project. Here are the steps to get TypeScript up and running.

4.1 Initialize a New Project

If you’re starting a new project, you can create a new directory for your project and initialize a package.json file by running the following commands:

mkdir my-typescript-project
cd my-typescript-project
npm init -y

This will generate a package.json file with default values, which you can modify later.

4.2 Create a TypeScript Configuration File

The next step is to create a TypeScript configuration file, tsconfig.json, which contains all the settings for your TypeScript project. You can create this file manually or use the tsc --init command to generate it automatically.

To create the file, run:

tsc --init

This will generate a tsconfig.json file with default settings. Here’s an example of a simple tsconfig.json file:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules"]
}
  • target: Specifies the JavaScript version to compile to (e.g., es5, es6).
  • module: Defines the module system (e.g., commonjs, es6).
  • strict: Enables all strict type-checking options.
  • include: Specifies which files or directories to include in the TypeScript project.
  • exclude: Specifies which files or directories to exclude.

4.3 Create TypeScript Files

Once your tsconfig.json file is set up, you can start writing TypeScript code. Create a new file with the .ts extension, for example, index.ts, and add the following TypeScript code:

let message: string = "Hello, TypeScript!";
console.log(message);

This is a simple program that defines a variable message with the type string and then logs it to the console.

4.4 Compile TypeScript to JavaScript

To compile your TypeScript code into JavaScript, run the following command in your terminal:

tsc

This will compile all .ts files in your project (based on the tsconfig.json settings) into .js files. By default, the compiled files will appear in the same directory as the source files.

You can also specify a single file to compile by providing its name:

tsc index.ts

4.5 Running the JavaScript Code

After compiling your TypeScript code to JavaScript, you can run the resulting JavaScript code using Node.js:

node index.js

You should see the output Hello, TypeScript! printed in your terminal.

5. Automating the Build Process

To make the development process more efficient, you can automate the TypeScript build process by adding a script to your package.json file.

5.1 Adding a Build Script

In your package.json, under the "scripts" section, add the following:

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

This allows you to run npm run build to compile your TypeScript files. Simply run:

npm run build

5.2 Watching Files for Changes

If you’re actively developing your project and want to automatically recompile your TypeScript files when they change, you can use the --watch flag:

tsc --watch

Or, you can add this to your package.json scripts:

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

Now, whenever you run npm run watch, TypeScript will automatically recompile your code whenever you make changes to your .ts files.

6. Conclusion

Setting up TypeScript in your project is quick and easy. Whether you’re working on a small project or a large application, TypeScript’s static typing and powerful tooling can help improve the quality of your code, reduce bugs, and enhance maintainability.

TypeScript Installation   TypeScript Setup   TypeScript Tutorial   JavaScript Development   TypeScript Guide  
TypeScript Installation   TypeScript Setup   TypeScript Tutorial   JavaScript Development   TypeScript Guide  
 Your First TypeScript Program: A Step-by-Step Guide
Why Use TypeScript? Top 5 Reasons for Beginners 

More Reading!

  1. Understanding TypeScript Decorators: A Step-by-Step Guide
  2. Master TypeScript Generics: A Beginner’s Guide with Real-World Examples
  3. TypeScript Tuples: What They Are and How to Use Them
  4. Mastering TypeScript Functions: The Ultimate Guide
  5. Understanding TypeScript Data Types with Examples
On this page:
1. What is TypeScript? 1.1 Why Use TypeScript? 2. Prerequisites 2.1 Node.js and npm 2.2 Text Editor or IDE 3. Installing TypeScript 3.1 Installing TypeScript Globally 3.2 Installing TypeScript Locally 4. Setting Up TypeScript in Your Project 4.1 Initialize a New Project 4.2 Create a TypeScript Configuration File 4.3 Create TypeScript Files 4.4 Compile TypeScript to JavaScript 4.5 Running the JavaScript Code 5. Automating the Build Process 5.1 Adding a Build Script 5.2 Watching Files for Changes 6. Conclusion
Follow me

I work on everything coding and technology

   
Mapagam
Mapagam is your go-to resource for all things related to frontend development. From the latest frameworks and libraries to tips, tutorials, and best practices, we dive deep into the ever-evolving world of web technologies.
Licensed under Creative Commons (CC BY-NC-SA 4.0).
 
Frontend
JavaScript 
Web Api 
TypeScript 
React 
Social
Linkedin 
Github 
Mapagam
Code copied to clipboard