📖 Scenario: You want to quickly run TypeScript code without compiling it manually. Using ts-node lets you run TypeScript files directly, like running JavaScript with node.This is useful when testing small scripts or learning TypeScript without extra setup.
🎯 Goal: Learn how to write a simple TypeScript file and run it directly using ts-node.
📋 What You'll Learn
Create a TypeScript file with a simple variable and a function
Use ts-node to run the TypeScript file directly
Print output to the console
💡 Why This Matters
🌍 Real World
Developers often want to quickly test TypeScript code snippets without compiling. ts-node helps run TypeScript files directly, speeding up development and learning.
💼 Career
Knowing how to run TypeScript code efficiently is useful for frontend and backend developers working with TypeScript in real projects.
Progress0 / 4 steps
1
Create a TypeScript file with a greeting variable
Create a variable called greeting and set it to the string "Hello from TypeScript!".
Typescript
Hint
Use const greeting: string = "Hello from TypeScript!"; to declare the variable.
2
Add a function to print the greeting
Add a function called printGreeting that takes no parameters and prints the greeting variable using console.log.
Typescript
Hint
Define function printGreeting(): void { console.log(greeting); }.
3
Call the function to display the greeting
Call the function printGreeting() to display the greeting message.
Typescript
Hint
Simply write printGreeting(); to call the function.
4
Run the TypeScript file using ts-node
Run the TypeScript file using the command ts-node filename.ts in your terminal to see the output.
Typescript
Hint
Open your terminal and run ts-node filename.ts where filename.ts is your file name.
Practice
(1/5)
1. What is the main purpose of using ts-node when working with TypeScript?
easy
A. To debug JavaScript code in the browser
B. To compile TypeScript files into JavaScript files only
C. To convert JavaScript files into TypeScript files
D. To run TypeScript files directly without compiling them first
Solution
Step 1: Understand what ts-node does
ts-node allows running TypeScript files directly without a separate compilation step.
Step 2: Compare options
The other options describe unrelated tasks like compiling only, converting JS to TS, or debugging JS.
Final Answer:
To run TypeScript files directly without compiling them first -> Option D
2. Which of the following is the correct command to run a TypeScript file named app.ts using ts-node?
easy
A. ts-node app.ts
B. ts-node app.js
C. node app.ts
D. tsc app.ts
Solution
Step 1: Identify the correct file extension for ts-node
ts-node runs TypeScript files, so the file must have a .ts extension.
Step 2: Check the commands
ts-node app.ts uses ts-node with the correct .ts file. ts-node app.js uses .js file which is incorrect. node app.ts uses node which cannot run .ts files directly. tsc app.ts runs the compiler but does not execute.
Final Answer:
ts-node app.ts -> Option A
Quick Check:
Run ts-node with .ts file = A [OK]
Hint: Use ts-node with .ts files, not .js [OK]
Common Mistakes:
Trying to run .js files with ts-node
Using node to run .ts files directly
Confusing tsc (compiler) with ts-node (runner)
3. What will be the output when running the following TypeScript code with ts-node?