Introduction

Completed

As a dynamically typed language, JavaScript makes declaring variables easy, but it can in some cases lead to unexpected results. The static type system in TypeScript enables you to describe the shape of an object, providing better documentation, and allowing TypeScript to validate that your code is working correctly. In TypeScript, declarations of namespaces, classes, properties, functions, variables, and other language entities associate types with those entities. The way that a type is formed and associated with a language entity depends on the kind of entity.

This module introduces some of the available types and shows how to associate them with variables. Later modules examine how interfaces, functions, and classes use static typing.

Learning objectives

After completing this module, you'll be able to:

  • Explain the advantages of declaring typed variables in TypeScript.
  • Declare variables using primitive types.
  • Declare variables using object types.
  • Declare variables using union and intersection types.

Prerequisites

  • Familiarity with JavaScript.
  • Familiarity with declaring variables using let and const in JavaScript.
  • Basic knowledge of TypeScript.
  • Installed software:
    • Git

    • Visual Studio Code

    • Node.js

    • TypeScript - TypeScript can be installed after installing Node.js, by running the following command in a terminal or command window:

      npm install -g typescript
      

For best results, follow the complete instructions for setting up your environment and using the TypeScript compiler in Module 01: Get started with TypeScript. After setting up your environment, you can run any of the Lab setup or solution files in the Build JavaScript applications by using TypeScript learning path.