Creating and Running .ts Files
This page explains how to save TypeScript code as a text file, compile (transpile) it to JavaScript, and run it. The file itself is simply a plain text file saved with a .ts extension.
How to write a .ts file
Write your TypeScript code in a text editor and save the file with a .ts extension. Save the file using the UTF-8 character encoding.
function greet(name: string): string {
return "Hello, " + name + "!";
}
const message: string = greet("TypeScript");
console.log(message);
TypeScript is a language that adds type information to JavaScript. You can specify types for variables, function parameters, and return values. TypeScript files cannot be run as-is; they must first be converted to JavaScript.
npx tsc hello.ts node hello.js Hello, TypeScript!
How to write comments
You can write comments (notes) in a .ts file. Comments are ignored by the compiler, so they are useful for leaving descriptions or notes about the code.
| Syntax | Description |
|---|---|
| // comment | A single-line comment. Write it with one space after //. Everything from // to the end of the line is a comment. |
| /* comment */ | A multi-line comment. Everything from /* to */ is a comment. |
function greet(name: string): string {
return "Hello, " + name + "!";
}
/*
Generates a message and
outputs it to the console.
*/
const message: string = greet("TypeScript");
console.log(message);
Running the above produces the following output:
npx tsc sample_comments.ts node sample_comments.js Hello, TypeScript!
How to run
TypeScript cannot be run as-is. Two steps are required: first convert it to a JavaScript file with the tsc command, then run it with Node.js.
Step 1: Compile the .ts file to a .js file
Use the npx tsc command to convert the TypeScript file to a JavaScript file.
npx tsc hello.ts
When compilation succeeds, hello.js is generated in the same directory.
Step 2: Run the generated .js file with Node.js
node hello.js Hello, TypeScript!
Run directly with ts-node
Using ts-node, you can combine compilation and execution into a single step. It is convenient for checking behavior during development.
npx ts-node hello.ts Hello, TypeScript!
Comparison of run methods
| Method | Command | Characteristics |
|---|---|---|
| tsc + node (2 steps) | npx tsc filename.ts → node filename.js | Suitable for production builds or when detailed configuration is needed. |
| ts-node (1 step) | npx ts-node filename.ts | Suitable for quick behavior checks during development. |
Summary
A .ts file is simply a plain text file. You can create one by writing TypeScript code in a text editor and saving it with a .ts extension.
Because TypeScript cannot be run directly, a conversion step to JavaScript is required. The basic flow is to compile with npx tsc and run the generated .js file with Node.js. For quick checks during development, using npx ts-node is convenient as it runs with a single command.
There are two types of comments: single-line comments (//) and multi-line comments (/* */). Single-line comments are used most often in day-to-day coding.
For recommended editors and environment setup, see Setup.
If you find any errors or copyright issues, please contact us.