How to Run TypeScript Files?

Running TypeScript files isn’t as easy as running JavaScript, but once you set up your environment, it becomes second nature.

In my years of developing enterprise-scale applications, I’ve found that knowing the right way to execute your code can save hours of debugging.

In this tutorial, I will show you exactly how to run TypeScript files using various methods that I use in my daily workflow.

Method 1: Use the TypeScript Compiler (tsc)

The most traditional way to run a TypeScript file is to compile it into JavaScript first using the built-in TypeScript Compiler (tsc).

I use this method whenever I need to ensure that my code is production-ready and fully type-checked.

Step 1: Install TypeScript

First, you need to have TypeScript installed on your machine. You can do this globally using npm.

Open your terminal and run:

npm install -g typescript

Step 2: Create a TypeScript File

Let’s create a file named salaryCalculator.ts. We will use a real-world example of calculating a standard 40-hour work week salary in the US.

// salaryCalculator.ts
const hourlyRate: number = 55;
const hoursPerWeek: number = 40;

function calculateWeeklyPay(rate: number, hours: number): number {
    return rate * hours;
}

const totalPay = calculateWeeklyPay(hourlyRate, hoursPerWeek);
console.log(`The total weekly gross pay is: $${totalPay}`);

Step 3: Compile and Run

Now, run the compiler to generate a JavaScript file:

tsc salaryCalculator.ts

This creates a salaryCalculator.js file in the same folder. You can now run it using Node.js:

node salaryCalculator.js

Method 2: Run TypeScript Directly with ts-node

Compiling every time can be a bit tedious during the development phase. That is why I prefer using ts-node.

It allows you to execute TypeScript files directly without manually generating a separate JavaScript file.

Step 1: Install ts-node

You can install ts-node along with TypeScript locally in your project or globally.

npm install -g ts-node

Step 2: Execute the File

Using the same salaryCalculator.ts file we created earlier, simply run:

ts-node salaryCalculator.ts

I find this incredibly helpful when I want to quickly test a logic block or a utility function without cluttering my workspace with .js files.

Method 3: Use Deno for Zero Configuration

If you want to skip the npm install dance entirely, Deno is a fantastic alternative.

Deno supports TypeScript out of the box, meaning you don’t need to install a compiler or a runner like ts-node.

Step 1: Install Deno

If you haven’t tried Deno yet, it’s a secure runtime for JavaScript and TypeScript created by the original creator of Node.js.

On Windows, you can install it via PowerShell:

irm https://deno.land/install.ps1 | iex

Step 2: Run the File

Once installed, running a file is as simple as:

deno run salaryCalculator.ts

I often use Deno for quick scripts because it handles the TypeScript execution environment automatically.

Method 4: Run TypeScript in Visual Studio Code

Since most of us spend our time in VS Code, running TypeScript directly from the editor is a huge productivity booster.

I use a specific extension called “Code Runner” to handle this with a single click.

Step 1: Install Code Runner Extension

Go to the Extensions view in VS Code (Ctrl+Shift+X) and search for “Code Runner”. Click install.

Step 2: Set up the Executor Map

To make sure it uses ts-node, go to your VS Code settings and search for executorMap.

Ensure that the .ts entry is set to ts-node.

Step 3: Click Play

Now, you can simply open your TypeScript file and click the “Play” button in the top right corner.

The output will appear immediately in the terminal window below.

Method 5: Use tsx (TypeScript Execute)

Recently, I have started using tsx in my projects. It is a newer, faster alternative to ts-node that uses esbuild under the hood.

It is particularly great for projects that use modern ESM (ECMAScript Modules).

Step 1: Install tsx

Run the following command to install it:

npm install -g tsx

Step 2: Run your code

tsx salaryCalculator.ts

In my experience, tsx feels much snappier than other runners, especially as your file size grows.

Real-World 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

You can see the output in the screenshot below.

how to run typescript file

Summary of Execution Methods

MethodBest ForRequirement
tscProduction / Build scriptsTypeScript Compiler
ts-nodeGeneral DevelopmentNode.js + ts-node
DenoQuick scripts / Modern appsDeno Runtime
Code RunnerEditor productivityVS Code Extension
tsxFast, modern ESM projectsNode.js + tsx

In this tutorial, I showed you five different ways to run your TypeScript files.

I personally recommend starting with ts-node for your daily development and using tsc when you are ready to deploy your code.

You may read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.