Typescript
Published in Typescript
avatar
2 minutes read

Running TypeScript Files from Command Line

To run TypeScript files (.ts) from the command line, you need to compile the TypeScript code into JavaScript (.js) first, and then execute the resulting JavaScript file using Node.js.

#1. Install TypeScript Compiler (tsc)

Before you can compile TypeScript files, make sure you have the TypeScript compiler (tsc) installed globally on your system. If you don't have it installed, you can install it using npm (Node Package Manager):

npm install -g typescript

#2. Write Your TypeScript Code

Create a new TypeScript file (e.g., app.ts) and write your TypeScript code in it.

// app.ts
const message: string = "Hello, TypeScript!";
console.log(message);

#3. Compile TypeScript to JavaScript

Use the TypeScript compiler (tsc) to compile your TypeScript code into JavaScript:

tsc app.ts

After running this command, you'll see a new file named app.js generated in the same directory. This file contains the transpiled JavaScript code from your TypeScript file.

#4. Run the JavaScript File

Now that you have the JavaScript file, you can execute it using Node.js:

node app.js

The output will be:

Hello, TypeScript!

0 Comment