TypeScript / TypeScript Build Tools and Configuration

Compiling TypeScript Code with tsc

This tutorial will introduce you to the TypeScript compiler (tsc) and its usage. You'll learn how to use the compiler to convert your TypeScript code into JavaScript that can be e…

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Covers configuring TypeScript projects, using build tools, and optimizing performance.

1. Introduction

In this tutorial, we aim to help you understand how to compile TypeScript code using the TypeScript compiler (tsc). By the end of this tutorial, you will learn how to convert your TypeScript code into JavaScript using the tsc compiler.

Prerequisites: Basic familiarity with TypeScript and JavaScript. You should also have Node.js and npm installed on your machine.

2. Step-by-Step Guide

Installing TypeScript

The TypeScript compiler can be installed globally on your machine via npm (Node Package Manager) using the following command:

npm install -g typescript

Compiling TypeScript to JavaScript

Once TypeScript is installed, you can compile a .ts file to .js by using the tsc command followed by the file name.

tsc filename.ts

This will generate a new file filename.js in the same directory.

3. Code Examples

Let's see a simple TypeScript example and its compiled JavaScript output.

Example 1: Simple TypeScript Code

// filename.ts
let message: string = 'Hello, TypeScript!';
console.log(message);

To compile this TypeScript file to JavaScript, run:

tsc filename.ts

This will create a new file filename.js with the following JavaScript code:

// filename.js
var message = 'Hello, TypeScript!';
console.log(message);

As you can see, TypeScript's type annotation has been removed in the JavaScript output, since JavaScript does not support static typing.

4. Summary

In this tutorial, we've learned how to compile TypeScript code to JavaScript using the tsc compiler. The next step would be to learn more advanced TypeScript features and how they translate into JavaScript.

You can refer to the official TypeScript documentation for more detailed information and additional resources.

5. Practice Exercises

  1. Create a TypeScript file that defines a function to add two numbers. Compile it to JavaScript using the tsc compiler.

Solution:

// add.ts
function add(a: number, b: number): number {
  return a + b;
}

console.log(add(2, 3));  // Outputs: 5

To compile, run: tsc add.ts

  1. Modify the above TypeScript file to use arrow functions. Compile it to JavaScript.

Solution:

// add_arrow.ts
const add = (a: number, b: number): number => {
  return a + b;
}

console.log(add(2, 3));  // Outputs: 5

To compile, run: tsc add_arrow.ts

Keep practicing different TypeScript concepts and compiling them to JavaScript to understand how they translate into JavaScript.

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

Hex to Decimal Converter

Convert between hexadecimal and decimal values.

Use tool

WHOIS Lookup Tool

Get domain and IP details with WHOIS lookup.

Use tool

MD5/SHA Hash Generator

Generate MD5, SHA-1, SHA-256, or SHA-512 hashes.

Use tool

Backlink Checker

Analyze and validate backlinks.

Use tool

Watermark Generator

Add watermarks to images easily.

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