Getting Started with TypeScript

Tutorial 1 of 5

1. Introduction

Tutorial's Goal

This tutorial aims to provide an introduction to TypeScript, a popular statically typed superset of JavaScript that adds optional types to JavaScript. By the end of this tutorial, you should be able to set up a TypeScript development environment, write a simple TypeScript program, and understand the TypeScript compilation process.

What You Will Learn

  • Setting up the TypeScript development environment
  • Writing your first TypeScript program
  • Understanding the TypeScript compilation process

Prerequisites

  • Basic knowledge in JavaScript programming.
  • Node.js and npm installed on your machine.

2. Step-by-Step Guide

Setting Up the TypeScript Development Environment

  1. First, you need to install TypeScript. You can do this by running the following command in your terminal:
    sh npm install -g typescript

Writing Your First TypeScript Program

  1. Create a new file hello.ts and add the following code:
    ts let message: string = 'Hello, TypeScript!'; console.log(message);
    The let keyword declares a variable in TypeScript. The : string annotation adds a type annotation to the variable. This is a specific feature of TypeScript - JavaScript does not have type annotations.

Understanding the TypeScript Compilation Process

  1. TypeScript is not understood by browsers. So, we need to compile our TypeScript code to JavaScript. Run the following command in your terminal:
    sh tsc hello.ts
    This command compiles your TypeScript code in hello.ts to JavaScript code in hello.js.

3. Code Examples

TypeScript Variable

Here's an example of a TypeScript variable with a type annotation:

let message: string = 'Hello, TypeScript!';

In this line of code, we declare a variable message with a type annotation string. TypeScript will now ensure that message always holds a string.

TypeScript Array

Here's an example of a TypeScript array:

let names: string[] = ['Jake', 'Amy', 'Holt'];

In this line of code, we declare a variable names with a type annotation string[]. The string[] type annotation ensures that names always holds an array of strings.

4. Summary

In this tutorial, we covered how to set up a TypeScript development environment, how to write a simple TypeScript program, and how to compile TypeScript code to JavaScript.

Next Steps

You can continue learning more about TypeScript by understanding more about types, functions, interfaces, and classes in TypeScript.

Additional Resources

5. Practice Exercises

  1. Write a TypeScript function that takes in a string and returns the string in reverse.
  2. Write a TypeScript function that takes in an array of numbers and returns the sum.

Solutions

Exercise 1

function reverseString(s: string): string {
  return s.split('').reverse().join('');
}
console.log(reverseString('TypeScript'));  // Output: 'tpircSepyT'

Exercise 2

function sumArray(numbers: number[]): number {
  return numbers.reduce((a, b) => a + b, 0);
}
console.log(sumArray([1, 2, 3, 4, 5]));  // Output: 15

Tips for Further Practice

Try to solve problems from coding platforms like LeetCode and HackerRank using TypeScript. This will not only improve your TypeScript skills but also your problem-solving skills.