How to Run TypeScript Files?

In this tutorial, I will explain how to run TypeScript files using various methods. As a developer, I faced a scenario where I needed to quickly test a new feature for a client project based in San Francisco. I had to ensure that my TypeScript code ran seamlessly without any issues. This guide will help you navigate different methods to execute TypeScript files efficiently.

To run a TypeScript file using the TypeScript compiler (tsc), first install the TypeScript compiler globally with npm install -g typescript. Then, compile your TypeScript file (e.g., app.ts) using the command tsc app.ts, which generates a corresponding JavaScript file (app.js). Finally, execute the compiled JavaScript file with Node.js by running node app.js. This method ensures your TypeScript code is properly compiled before execution.

Run TypeScript Files

TypeScript is a powerful, statically typed superset of JavaScript that adds optional static types, classes, and interfaces. It helps developers catch errors early and write more robust code. Running TypeScript files involves compiling them to JavaScript and then executing the JavaScript code.

Now, let me show you different methods to run TypeScript files.

Method 1: Using the TypeScript Compiler (tsc)

The most common way to run TypeScript files is by using the TypeScript compiler (tsc). This method involves two steps: compiling the TypeScript file into JavaScript and then running the JavaScript file with Node.js.

Step-by-Step Guide

  1. Install TypeScript Compiler:
    First, ensure you have TypeScript installed. You can install it globally using npm:
   npm install -g typescript
  1. Compile the TypeScript File:
    Use the tsc command to compile your TypeScript file. For example, if you have a file named app.ts:
   tsc app.ts
  1. Run the Compiled JavaScript File:
    After compilation, a JavaScript file (app.js) will be generated. Run this file using Node.js:
   node app.js

This straightforward method ensures that your TypeScript code is properly compiled before execution.

Check out Try Catch in TypeScript

Method 2: Using ts-node

For a more streamlined approach, you can use ts-node, which allows you to run TypeScript files directly without the need for a separate compilation step.

Step-by-Step Guide

  1. Install ts-node:
    Install ts-node using npm:
   npm install -g ts-node
  1. Run the TypeScript File Directly:
    With ts-node, you can run your TypeScript file directly:
   ts-node app.ts

This method is particularly useful for quick testing and development, as it eliminates the need for manual compilation.

Check out How to Handle Catch Error Types in TypeScript?

Method 3: Using Deno

Deno is a modern runtime for JavaScript and TypeScript that provides a secure and simple way to run TypeScript files directly.

Step-by-Step Guide

  1. Install Deno:
    Follow the installation instructions on the Deno website.
  2. Run the TypeScript File:
    Use the deno run command to execute your TypeScript file:
   deno run app.ts

Deno simplifies the process by natively supporting TypeScript, making it an excellent choice for developers looking for a modern runtime.

Check out How to Check TypeScript Version?

Real-World Example

Now, let me show you a real example.

Imagine you are developing a feature for a client in New York that involves processing user data. You have a TypeScript file processData.ts that performs this task. Using ts-node, you can quickly test your code:

// processData.ts
interface User {
    name: string;
    age: number;
    city: string;
}

const user: User = {
    name: "John Doe",
    age: 30,
    city: "New York"
};

console.log(`Processing data for ${user.name} from ${user.city}`);

Run the file directly with ts-node:

ts-node processData.ts

This will output:

Processing data for John Doe from New York

Here is the exact output in the screenshot below:

how to run typescript file

Conclusion

In this tutorial, I explained three different methods to run TypeScript files, such as the TypeScript compiler, ts-node, or Deno. I hope you understand how to run TypeScript files.

You may also like: