TypeScript Tutorial

TypeScript is typed JavaScript. TypeScript adds types to JavaScript to help you speed up development by catching errors before you even run the JavaScript code.

TypeScript is an open-source programming language that builds on top of JavaScript. It works on any web browser, any OS, and any environment where JavaScript runs.

In this TypeScript tutorial, you’ll learn:

  • Why TypeScript offers a lot of benefits over vanilla JavaScript.
  • Understand what TypeScript truly is and how it works under the hood.
  • Use TypeScript and its rich features like Types, Classes, Interfaces, Modules, and more.

Prerequisites

To follow the tutorial, you need to have:

Section 1. Getting Started #

Section 2. Basic Types #

  • Type Annotation – learn how to use type annotation to define the static type for variables, function parameters, and return values.
  • Type Inference – explain where and how TypeScript infers types of variables.
  • Number – learn about the numeric types, including floating-point numbers and big integers.
  • String – show you how to use the string type in TypeScript.
  • Boolean – guide you on the boolean type and how to use it effectively.
  • Object Type – introduces you to the object type that represents non-primitive values.
  • Array – show you how to use an array and its operations.
  • Tuple – learn how to store a fixed number of elements with known types in a tuple.
  • Unknown Type – show you how to create a variable that can hold a value of any type but requires type checking before use.
  • Any Type – learn how to use the any type to store a value of any type in a variable.
  • Enum – show you how to define a group of named constants using the enum type.
  • Void type – show you how to use the void type as the return type of functions that do not return any value.
  • Union Types – guide you on how to store a value of one of several types in a variable with the union type.
  • String Literal Types – learn to define a type that accepts only a specified string literal.
  • Never Type – learn how to use the never type that contains no value.
  • Type Aliases – show you how to define new names for types using type aliases.

Section 3. Control Flow Statements #

The following tutorials refresh your control flow statement knowledge. If you are already familiar with these statements in JavaScript, please skip this section.

  • if…else – learn how to execute code based on a condition.
  • switch..case – show you how to use the switch statement to allow some of the possible execution paths.
  • for – create a loop that executes a specified number of times.
  • while – create a pretest loop that executes as long as a condition is true.
  • do…while – learn how to create a posttest loop that executes until a condition is false.
  • break – show you how to use the break statement to terminate a loop or a switch.
  • continue – learn how to skip to the end of a loop and continue to the next iteration.

Section 4. Functions #

  • Functions – learn how to declare a function that uses type annotation in TypeScript.
  • Function types – guide you on how to use function types to define types for functions.
  • Optional Parameters – show you how to define functions with optional parameters.
  • Default Parameters – introduce you to default parameters.
  • Rest Parameters – use rest parameters to handle an infinite number of arguments of a function.
  • Function Overloading – learn how to establish different parameter types and results of a function via function overloading.

Section 5. Classes #

  • Classes – learn how to construct a class in TypeScript.
  • Access Modifiers – introduce you to private, protected, and public access modifiers.
  • The readonly Modifier – learn how to use the readonly modifier to make class properties immutable.
  • Getters & Setters – show you how to use getters and setters to control access of the class properties.
  • Inheritance – learn how to use inheritance to reuse the functionality of another class.
  • Static methods & properties – define static methods and variables shared by all instances of a class.
  • Abstract Classes – explain the abstract classes that define some common behaviors.

Section 6. Interfaces #

Section 7. Advanced Types #

  • Intersection Types – show you how to create a new type that has all the features of existing types.
  • Type Guards – learn how to use type guards to narrow a variable’s type within a conditional block.
  • Type Assertions – learn how to use type assertions to tell the compiler to treat a value as a specified type.

Section 8. Generics #

Section 9. Type Operators #

Section 10. Advanced Types #

  • Mapped Types – create a new object type by transforming existing object types.
  • Conditional Types – select one type or another based on a type relationship.

Section 11. Modules #

  • TypeScript Modules – introduce you to modules in TypeScript and how to use them in other modules.

Section 12. Setting up TypeScript tools #

  • Node.js TypeScript – how to set up a development environment for using TypeScript in a Node.js project.
  • TypeScript Vite – set up a web project that uses TypeScript and Vite to compile the TypeScript into JavaScript.