Mapagam
  • JavaScript 
  • Web APIs 
  • TypeScript 
  • React 

Your First TypeScript Program: A Step-by-Step Guide

Posted on March 31, 2025 • 6 min read • 1,136 words
Share via
Mapagam
Link copied to clipboard

Learn how to write your first TypeScript program with this step-by-step guide, covering setup, syntax, and key TypeScript features.

On this page
1. Introduction to TypeScript 1.1 What is TypeScript? 1.2 Why Should You Learn TypeScript? 2. Setting Up TypeScript 2.1 Installing Node.js 2.2 Installing TypeScript 2.3 Setting Up Your Code Editor 3. Writing Your First TypeScript Program 3.1 Creating a TypeScript File 3.2 Writing TypeScript Code 3.3 Understanding TypeScript Syntax 4. Compiling TypeScript to JavaScript 4.1 Compiling TypeScript 4.2 Understanding the Compiled JavaScript 4.3 Running the JavaScript Code 5. Key TypeScript Features to Explore 5.1 Static Typing 5.2 Interfaces 5.3 Classes and Inheritance 5.4 Type Aliases 6. Conclusion

TypeScript is a powerful, statically typed superset of JavaScript that brings a wealth of advantages to web development, including type safety, enhanced tooling, and better code readability.

1. Introduction to TypeScript

Before diving into writing your first TypeScript program, it’s essential to understand what TypeScript is and why you should use it.

1.1 What is TypeScript?

TypeScript is an open-source programming language developed by Microsoft that builds on JavaScript by adding optional static typing and other advanced features. It allows developers to catch errors at compile-time rather than runtime, providing a more predictable and safer development experience.

Unlike JavaScript, which is dynamically typed, TypeScript’s static typing can help you prevent common bugs in your code. It also enables better tooling and offers enhanced code completion, navigation, and refactoring support in IDEs like Visual Studio Code.

1.2 Why Should You Learn TypeScript?

There are several reasons why TypeScript has become a go-to language for modern web development:

  • Improved Code Quality: TypeScript’s static typing ensures that developers can detect type-related errors early in the development process.
  • Better Developer Experience: TypeScript integrates seamlessly with modern IDEs, offering features like auto-completion, code suggestions, and error checking.
  • Enhanced Tooling: TypeScript supports modern JavaScript features and provides tools for code refactoring, making it ideal for large-scale projects.
  • Community and Ecosystem: TypeScript is widely adopted, and most modern frameworks and libraries, such as Angular, React, and Vue.js, offer TypeScript support out of the box.

With that in mind, let’s start by writing your first TypeScript program!

2. Setting Up TypeScript

Before you can start coding in TypeScript, you’ll need to set up your development environment. This process involves installing Node.js, TypeScript, and a code editor.

2.1 Installing Node.js

TypeScript runs on top of Node.js, so you’ll need to have Node.js installed. Follow these steps to install Node.js:

  1. Go to the official Node.js website: https://nodejs.org/
  2. Download the latest version of Node.js (LTS version recommended).
  3. Follow the installation instructions for your operating system.

To verify the installation, open a terminal (or command prompt) and run:

node -v

This will print the installed version of Node.js.

2.2 Installing TypeScript

Once Node.js is installed, you can easily install TypeScript globally using npm (Node Package Manager):

npm install -g typescript

To verify the installation, run:

tsc -v

This will print the version of TypeScript that was installed.

2.3 Setting Up Your Code Editor

For TypeScript development, it’s highly recommended to use Visual Studio Code (VS Code), as it has built-in support for TypeScript.

  1. Download and install VS Code.
  2. Open VS Code and install the TypeScript plugin (if it’s not already installed).

Now that your development environment is set up, you’re ready to write your first TypeScript program!

3. Writing Your First TypeScript Program

Let’s break down the process of writing a simple TypeScript program that prints “Hello, TypeScript!” to the console.

3.1 Creating a TypeScript File

Start by creating a new folder for your project. Inside this folder, create a new file with a .ts extension (e.g., app.ts). The .ts extension signifies that the file contains TypeScript code.

3.2 Writing TypeScript Code

Now, open the app.ts file and add the following code:

// app.ts
function greet(name: string): string {
  return `Hello, ${name}! Welcome to TypeScript.`;
}

let name = "Alice";
console.log(greet(name));

In this code:

  • The greet function takes a parameter name of type string and returns a greeting message.
  • We call the greet function with the string "Alice" and print the result to the console using console.log().

3.3 Understanding TypeScript Syntax

Here are a few key TypeScript concepts used in the code:

  • Type Annotations: The function parameter name: string specifies that the name argument must be a string. This is TypeScript’s type annotation feature, which ensures that only valid types are passed to the function.
  • Type Inference: TypeScript can infer the type of variables automatically based on the value assigned. In the line let name = "Alice";, TypeScript automatically infers that name is a string.

4. Compiling TypeScript to JavaScript

Since browsers only understand JavaScript, you need to compile your TypeScript code into JavaScript. TypeScript provides a compiler (tsc) for this purpose.

4.1 Compiling TypeScript

To compile the app.ts file into JavaScript, open your terminal, navigate to the folder containing the app.ts file, and run:

tsc app.ts

This will generate a new file called app.js in the same folder, which contains the JavaScript version of your code.

4.2 Understanding the Compiled JavaScript

Open the app.js file, and you’ll see the compiled JavaScript code:

// app.js
function greet(name) {
    return "Hello, " + name + "! Welcome to TypeScript.";
}

var name = "Alice";
console.log(greet(name));

Notice how TypeScript has removed the type annotations (name: string) and converted the TypeScript code into regular JavaScript.

4.3 Running the JavaScript Code

To run the JavaScript code, simply use Node.js:

node app.js

This will output:

Hello, Alice! Welcome to TypeScript.

Congratulations! You’ve just written, compiled, and run your first TypeScript program.

5. Key TypeScript Features to Explore

Now that you’ve written a simple TypeScript program, let’s explore some key features of TypeScript that you can use to improve your coding experience.

5.1 Static Typing

Static typing is one of the primary benefits of TypeScript. You can explicitly specify types for variables, function parameters, and return values. Here are some examples:

Basic Types

let age: number = 25;
let isActive: boolean = true;
let username: string = "Alice";

Arrays and Tuples

let numbers: number[] = [1, 2, 3];
let tuple: [string, number] = ["Alice", 25];

Enums

Enums allow you to define a set of named constants:

enum Color {
  Red,
  Green,
  Blue
}

let favoriteColor: Color = Color.Green;

5.2 Interfaces

TypeScript allows you to define custom types using interfaces. Interfaces are especially useful for defining the shape of objects.

interface Person {
  name: string;
  age: number;
}

let person: Person = {
  name: "Alice",
  age: 25
};

5.3 Classes and Inheritance

TypeScript supports object-oriented programming with classes and inheritance. Here’s an example:

class Animal {
  constructor(public name: string) {}

  speak() {
    console.log(`${this.name} makes a noise.`);
  }
}

class Dog extends Animal {
  speak() {
    console.log(`${this.name} barks.`);
  }
}

let dog = new Dog("Buddy");
dog.speak();

5.4 Type Aliases

Type aliases allow you to create a new name for a type. This is useful for complex types or reusable type definitions.

type Point = { x: number; y: number };

let point: Point = { x: 10, y: 20 };

6. Conclusion

TypeScript is a powerful tool that enhances JavaScript development by providing static typing, better tooling, and improved code quality. By following this guide, you’ve written your first TypeScript program, compiled it into JavaScript, and run it using Node.js.

As you continue to explore TypeScript, you’ll uncover more features such as generics, decorators, and modules, which will help you write cleaner, more maintainable code. Embrace TypeScript for your next web development project, and you’ll quickly see the benefits it brings to your workflow.

TypeScript   JavaScript   Programming Guide   Web Development   TypeScript Tutorial  
TypeScript   JavaScript   Programming Guide   Web Development   TypeScript Tutorial  
 Understanding TypeScript Data Types with Examples
How to Install and Set Up TypeScript in Minutes 

More Reading!

  1. How TypeScript’s Type Inference Works (And Why It’s a Game-Changer)
  2. TypeScript Type Assertions: When and How to Use Them Effectively
  3. How to Extend Interfaces in TypeScript
  4. What Is the Nullish Coalescing Operator (??) in JavaScript?
  5. Short-Circuiting in JavaScript: Master Logical Operators Like a Pro
On this page:
1. Introduction to TypeScript 1.1 What is TypeScript? 1.2 Why Should You Learn TypeScript? 2. Setting Up TypeScript 2.1 Installing Node.js 2.2 Installing TypeScript 2.3 Setting Up Your Code Editor 3. Writing Your First TypeScript Program 3.1 Creating a TypeScript File 3.2 Writing TypeScript Code 3.3 Understanding TypeScript Syntax 4. Compiling TypeScript to JavaScript 4.1 Compiling TypeScript 4.2 Understanding the Compiled JavaScript 4.3 Running the JavaScript Code 5. Key TypeScript Features to Explore 5.1 Static Typing 5.2 Interfaces 5.3 Classes and Inheritance 5.4 Type Aliases 6. Conclusion
Follow me

I work on everything coding and technology

   
Mapagam
Mapagam is your go-to resource for all things related to frontend development. From the latest frameworks and libraries to tips, tutorials, and best practices, we dive deep into the ever-evolving world of web technologies.
Licensed under Creative Commons (CC BY-NC-SA 4.0).
 
Frontend
JavaScript 
Web Api 
TypeScript 
React 
Social
Linkedin 
Github 
Mapagam
Code copied to clipboard