Declaring Variables and Working with Data Types

Tutorial 4 of 5

Introduction

Our goal in this tutorial is to introduce you to the basics of declaring variables and working with different data types in TypeScript. TypeScript is a strongly typed, object-oriented, compiled language.

By the end of this tutorial, you will learn:

  • How to declare variables in TypeScript
  • The different data types available in TypeScript
  • How to use type assertions and interface types

This tutorial is beginner-friendly. A basic knowledge of JavaScript would be beneficial but is not strictly necessary.

Step-by-Step Guide

Declaring Variables

In TypeScript, you can declare variables using var, let, and const keywords, similar to modern JavaScript.

let name: string = "John Doe";
const age: number = 30;
var isAdult: boolean = true;

Data Types

TypeScript includes several data types, including:

  • Basic types: boolean, number, string, null, undefined, symbol, and void.
  • User-defined types: enum, class, interface, array, and tuple.

Type Assertions

TypeScript allows you to override its inferred and analyze your code similar to other statically typed languages. This is known as "Type Assertions". Type assertions have two forms.

// angle-bracket syntax
let str: any = "I am a string";
let strLength: number = (<string>str).length;

// as-syntax
let str: any = "I am a string";
let strLength: number = (str as string).length;

Interface Types

Interfaces are a powerful way to define contracts within your code and contracts with code outside of your project.

interface Person {
  firstName: string;
  lastName: string;
}

// This object is of type 'Person'
let aPerson: Person = { firstName: "John", lastName: "Doe" };

Code Examples

Example 1: Declaring Variables

let isDone: boolean = false; // Boolean type
let age: number = 20; // Number type
let fullName: string = "John Doe"; // String type

console.log(isDone); // Output: false
console.log(age); // Output: 20
console.log(fullName); // Output: John Doe

Example 2: Type Assertions

let value: any = "John Doe";
let length: number = (value as string).length;

console.log(length); // Output: 8

Example 3: Interface Types

interface Person {
  firstName: string;
  lastName: string;
}

let aPerson: Person = { firstName: "John", lastName: "Doe" };

console.log(aPerson.firstName); // Output: John
console.log(aPerson.lastName); // Output: Doe

Summary

In this tutorial, we've learned about declaring variables and using different data types in TypeScript. We also covered how to use type assertions and interface types. The next step would be learning about classes and objects in TypeScript. You can refer to the official TypeScript documentation for more information.

Practice Exercises

  1. Exercise: Declare a variable of type number and a variable of type boolean in TypeScript.

Solution:
typescript let age: number = 25; let isAdult: boolean = true;

  1. Exercise: Use type assertion to find the length of a string stored in a variable of type any.

Solution:
typescript let value: any = "Hello World"; let length: number = (value as string).length; console.log(length); // Output: 11

  1. Exercise: Create an interface for a Car object with model, year, and color properties.

Solution:
```typescript
interface Car {
model: string;
year: number;
color: string;
}

let myCar: Car = { model: "Toyota", year: 2015, color: "red" };
console.log(myCar); // Output: { model: 'Toyota', year: 2015, color: 'red' }
```

Happy coding!