Your First TypeScript Program
Learn how to create a TypeScript file, declare variables, display output, compile your program, and execute the generated JavaScript.
What Will We Create?
We will create a simple program using variables and
console.log(). This is a useful first
exercise for learning TypeScript syntax.
- Create a file named
app.ts. - Declare variables with explicit types.
- Display information using
console.log(). - Compile the TypeScript file.
- Run the generated JavaScript using Node.js.
First Program Workflow
Follow these steps to understand how your first program moves from source code to output.
Step 1
Write CodeCreate the app.ts file.
Step 2
CompileUse the TypeScript compiler.
Step 3
Generate JavaScriptProduce app.js.
Step 4
Run ProgramExecute using Node.js.
1. Create a TypeScript File
Open your project folder in Visual Studio Code and create
a new file called app.ts.
File Name
app.ts
.ts extension is used for TypeScript
source files. The compiler can generate a corresponding
JavaScript file.
2. Write Your First TypeScript Code
Enter the following code into app.ts.
It declares a student's name, age, and course.
Example
let studentName: string = "Rahul";
let age: number = 22;
let courseName: string = "TypeScript";
console.log("Student Name: " + studentName);
console.log("Age: " + age);
console.log("Course: " + courseName);
Expected Output
Student Name: Rahul
Age: 22
Course: TypeScript
The program displays three lines. The values shown here are the expected result when the code runs successfully.
3. Understand Each Line
| Code | Explanation |
|---|---|
let studentName |
Declares a variable to store the student's name. |
: string |
Specifies that the variable should contain a string value. |
: number |
Specifies that the variable should contain a numeric value. |
= |
Assigns a value to the variable. |
console.log() |
Writes information to the JavaScript console or Node.js terminal. |
4. Compile TypeScript
Open the terminal in the folder that contains
app.ts. If TypeScript is installed locally
in the project, use this command.
Command
npx tsc app.ts
If the TypeScript compiler is installed globally and
available in PATH, you can also use
tsc app.ts.
Generated File
app.ts
app.js
app.js. The output location can vary with
compiler configuration.
5. Run the Generated JavaScript
Execute the generated JavaScript using Node.js.
Command
node app.js
Output
Student Name: Rahul
Age: 22
Course: TypeScript
Practical Example: CIIT Student Details
Let's use TypeScript to display information about a student interested in a course at CIIT Training Institute.
Example
interface Student {
id: number;
name: string;
course: string;
}
const student: Student = {
id: 101,
name: "Priya",
course: "Dot Net Full Stack"
};
console.log("Student ID: " + student.id);
console.log("Student Name: " + student.name);
console.log("Selected Course: " + student.course);
Expected Output
Student ID: 101
Student Name: Priya
Selected Course: Dot Net Full Stack
The interface describes the expected structure of the student object, including the ID, name, and course.
Common Mistakes
-
Forgetting to save
app.tsbefore compiling. - Running commands from a folder that does not contain the expected file.
-
Trying to run
app.jsbefore it has been generated. - Using an incompatible value for a declared type.
- Confusing TypeScript source code with generated JavaScript.
Summary
In this lesson, we created a TypeScript file, declared typed variables, displayed output, compiled the file into JavaScript, and executed the result with Node.js. The workflow diagram summarises the main steps you will follow in future lessons.