TypeScript / TypeScript Basics

Compiling TypeScript to JavaScript

This tutorial covers the process of compiling TypeScript code into JavaScript, which can be understood by the browser. You will also learn about the configuration options that you…

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Covers the fundamental concepts of TypeScript, including type annotations, variables, and basic syntax.

Introduction

The goal of this tutorial is to teach you how to compile TypeScript code into JavaScript. TypeScript is a strictly syntactical superset of JavaScript, which adds optional static typing to the language. However, browsers don't understand TypeScript, so we need to compile it into JavaScript.

By the end of this tutorial, you will be able to:

  1. Write TypeScript code.
  2. Compile TypeScript code to JavaScript.
  3. Configure the TypeScript compiler.

You should have a basic knowledge of JavaScript to follow along with this tutorial.

Step-by-Step Guide

To compile TypeScript into JavaScript, we first need to install TypeScript. We can do this globally using npm (Node Package Manager) with the following command:

npm install -g typescript

After installing TypeScript, you can use the tsc command to compile your TypeScript files into JavaScript. For example, if you have a file named main.ts, you can compile it with:

tsc main.ts

This will create a main.js file in the same directory.

You can also configure the TypeScript compiler by creating a tsconfig.json file in your project's root directory. This file specifies the root files and the compiler options required to compile the project.

Here is an example tsconfig.json file:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "outDir": "./dist",
    },
    "include": [
        "./src/**/*"
    ]
}

In this configuration:

  • target specifies the ECMAScript target version. In this case, we are targeting ES5 for maximum browser compatibility.
  • module specifies the module code generation. Here, we're using CommonJS.
  • outDir specifies the output directory for the compiled JavaScript files. Here, we're outputting to a directory named dist.
  • include specifies the files or directories that should be included in the compilation.

Code Examples

Let's create a simple TypeScript file and compile it to JavaScript.

Create a file named main.ts with the following content:

// This is a simple TypeScript function
function greet(name: string) {
    return "Hello, " + name;
}

// Call the function
console.log(greet("TypeScript"));

In this file, we define a function named greet that takes a name parameter of type string. We then call this function and log the result to the console.

To compile this file, we use the tsc command:

tsc main.ts

This will create a main.js file with the following content:

function greet(name) {
    return "Hello, " + name;
}
console.log(greet("TypeScript"));

As you can see, the TypeScript code has been compiled into JavaScript.

Summary

In this tutorial, you learned how to compile TypeScript code into JavaScript. You also learned how to configure the TypeScript compiler using a tsconfig.json file.

The next step is to start writing more complex TypeScript code and compile it to JavaScript. You can also explore more configuration options in the TypeScript documentation.

Practice Exercises

  1. Write a TypeScript function that adds two numbers and compile it to JavaScript.
  2. Write a TypeScript class with a method and compile it to JavaScript.
  3. Create a tsconfig.json file with different configuration options and compile your TypeScript files.

Each of these exercises will give you practical experience with TypeScript and its compilation process. Remember to test your JavaScript output to ensure it behaves as expected.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Markdown to HTML Converter

Convert Markdown to clean HTML.

Use tool

QR Code Generator

Generate QR codes for URLs, text, or contact info.

Use tool

JWT Decoder

Decode and validate JSON Web Tokens (JWT).

Use tool

Fake User Profile Generator

Generate fake user profiles with names, emails, and more.

Use tool

Random Name Generator

Generate realistic names with customizable options.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help