Introduction to TypeScript - Part 2

Introduction to TypeScript - Part 2

Overview

In this article, we will talk about the basic data types we use in TypeScript, along with some examples. If you haven't read the first part of this article, where we did a deep dive into TypeScript and the problems it solves for JavaScript developers, you can click here to view it.

Introduction:

A type is a concept that describes the values that can be assigned to a variable or the values that can be passed into a function. JavaScript provides dynamic typing which means the code is run to determine the behavior, while TypeScript provides static typing which means TypeScript checks the type at compile time.

Data Types

The data types supported by TypeScript can be classified into built-in and user-defined types.

Examples of built-in data types include:

  • String

  • Number

  • Boolean

  • Undefined

  • Null

  • Symbol

Examples of user-defined types include:

  • Classes

  • Interfaces

  • Arrays

  • Tuples

  • Objects

  • Enums

For this article, we will be focusing on the built-in data types.

String:

A string is a sequence of characters. They are the most commonly used data types. Strings can be names, email addresses, or mailing addresses.

let name: string = "John";

Number:

Like JavaScript, a number is stored in TypeScript as a double precision floating point format value. In simple terms, numbers are stored as floating point numbers. Number values can include integers (e.g., 10), decimals (e.g., 1.25), and negative numbers (e.g., -8.23).

let age: number = 23;

Boolean:

The boolean data type can hold only two values: true or false. It is important to note that “Boolean” is different from boolean type. While “Boolean” is an object type, boolean is a primitive type.

let isAdmin: boolean = false;

Undefined:

TypeScript, like JavaScript, when a variable is declared but without a value, the variable has the value of undefined. The type of the variable is also considered undefined.

let email: string;
console.log(typeof email); // returns "undefined"

Null:

A null value simply means that there a variable has no assigned value. Note that null is not equivalent to “undefined” or an empty string (””).

let nullVar = null;

Symbol:

Symbols are immutable and unique values that can be created using the Symbol constructor or the Symbol() function. Symbols are often used as keys in objects to ensure uniqueness.

let sym = Symbol("value");

Note: TypeScript uses type inference to provide type information when there is no explicit type annotation.

Conclusion

In this article, we explored the basic built-in data types in TypeScript, including string, number, boolean, undefined, null and symbol. Understanding these data types is crucial for writing type-safe and reliable TypeScript code. In the next article, we will delve into user-defined types such as classes, interfaces, arrays, tuples, objects and enums. Stay tuned!

Note: This article is a continuation of the previous article. If you haven’t read the first part make sure to check it out here.

Thank you for joining me on this technical journey. Your support and interest mean the world to me. I look forward to sharing more insightful content with you in the future. Until then, stay curious and keep exploring!