Built-in types in TypeScript

Learn via video courses
Topics Covered

Overview

A data type is a classification of a variable representing the type of data it can hold, such as strings, numbers, boolean, etc. For example, if we have a string then we can declare it using the string keyword.

Now let's read about the typescript data types- In typescript there are two data types :

  1. Built-in types
  2. User-defined types In this article, we will explore the built-in types in detail.

Introduction

All of us have written a simple program to add two numbers and have used the data type number, what does that mean it's the data type of the variable we are declaring. We need to define the data type of the variable not only in typescript but also in other languages like C, C++, java, etc. Similarly in Typescript, we can declare data types for variables and functions. When creating a variable, there are two main ways TypeScript assigns a type:

  1. Explicit - Manually writing the data type
  2. Implicit - Data type which typescript compiler infers based on the value stored in the variable

Let's dive deeper into the built-in typescript data types in detail:

  1. Number
  2. String
  3. Boolean
  4. Void
  5. Null
  6. Undefined
  7. Any

1.Number:

It consists of whole numbers and floating point values, it is represented by the keyword number. Let's see an example below:

OUTPUT

In the above code, we have declared the variable named a, and since we are going to store an arithmetical value in it so we are using the data type number.Run the code in your editor for a clear explanation.

2.String:

The string is another built-in typescript data type. The values in a string are surrounded by single quotation marks or double quotation marks. It is represented by the keyword string. Let's see an example below:

OUTPUT

In the above code, we have declared a variable named name1 and name2 since we are going to store text data in it so we are using the data type string.

3.Boolean

Boolean is another built-in typescript data type, it represents the true or false values. It is represented by the keyword boolean. Now let's see an example below:

OUTPUT

In the above code, we have declared a variable named a and b since we are going to check whether the variables are true or false so we are using the data type boolean. Run the code in your editor for a clear explanation.

Built In Data Types

4.Void:

The void type is used when there is no data, it is used when functions return no value. It is represented by the void keyword Let's see an example of when it will throw an error:

OUTPUT

If we assign a variable using the void data type only undefined can be assigned to it.No other data types can be assigned to the variable when we declare the variable using the void data type. We can also use void as the return type of those function that does not return any value. Run the code in your editor for a clear explanation.

Let's see another example of when the void is useful:

OUTPUT:

As we know there is no meaning to assigning void to a variable, as only null or undefined is assignable to void.

5.Null:

Null refers to the absence of any object value. It means nothing or no value. It is similar to the void type but we have to define it explicitly. The null keyword is used to define the null type in typescript. Let's see an example:

OUTPUT:

In this code, we have created a variable named flag and set it to null, then we assigned the value 10 to the variable flag which is throwing an error because no other data type can be assigned to the variable when we have set the variable as null. Run the code in your editor for a clear explanation.

Now let us see another example where we are assigning null to values:

OUTPUT:

When we were printing an empty string the output was undefined but when we are assigning null to the variable then the output is null.

6.Undefined:

Undefined is another primitive typescript data type that denotes all uninitialized variables in typescript. Assigning a value to an undefined data type is of no use. Undefined can only be assigned to variables with any data type and undefined data type. Let's see an example:

OUTPUT:

In the above code, we have declared a variable named name1 and then set it to undefined, and then we were assigning a string type data to it which is throwing an error because undefined can only be assigned to variables with any data type, and undefined data types. Run the code in your editor for a clear explanation.

7.Any:

Typescript has a special data type that doesn't refer to any specific data type that is. The Any data type is used when we don't know the type of the variable we are declaring. It is especially used in user-entered values. It is declared by the keyword any. Using any typescript cannot provide us with type safety. Setting the variable as any disables type checking. Let's see an example:

OUTPUT:

In the above code, we have declared two variables a and b, and set them to any and they are assigning string and numeric values in it which will not throw any error as the variables are set as any, and any disables type checking. This behavior is similar to javascript's variable Run the code in your editor for a clear explanation.

Conclusion

  1. In this article we have learned the built-in typescript data types, which will be frequently used when we write a typescript program.
  2. Declaring the data types explicitly makes it easier to read and understand the code but sometimes it is easy to just let the compiler infer the type.