Table of contents
1.
Introduction
2.
Built-in Data Types
2.1.
Number
2.2.
String
2.3.
Boolean
2.4.
Void
2.5.
Null
2.6.
Undefined
2.7.
Any
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024

Data Types in Typescript

Author Parth Jain
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A variable is like a container that stores data inside it. As we have a shoebox for storing shoes and a jewelry box for storing jewelry, similarly in programming, defining the type of data held in the container is essential. The exact type of container is used for a particular type of data. 

Therefore, It is important to acquaint ourselves with the various data types used in Typescript.

Typescript consists of two major Data Types: 

  1. Built-in Data Types
  2. User-defined Data Types

Built-in Data Types

As the name suggests, these are the primitive data types provided as default by Typescript. Currently, Typescript supports about 7 data types.

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

Number

This data type stores both integer and floating-point values (decimals). Furthermore, this data type can store hexadecimal, octal and binary values.

let int: number = 9;          //Stores Integer Number
let dec_num: number = 3.414;  //Stores Decimal Number
let hex: number = 0x37CF;     //Stores Hexadecimal 
let bin: number = 0b111001;   //Stores Binary 
let oct: number = 0o377;      //Stores Octal Number

String

This data type stores a collection of characters, commonly known as a string of characters. 

let deptName: string = “Coding Ninjas”; //Using Double Quotes
let empName: string = ‘Happy Singh’;    //Using Single Quotes
let empID: string = `CNHS`;             //Using Backtick

 

The use of Single, Double, and Backticks does not change the working of the code. However, backticks, also known as String Literals, can be used to produce multiple line spans and embed expressions by using  ${variable_name }.

let literal_testing: string = `My name is ${empName}.
 
I work at ${deptName}.`;

Boolean

This data type is used to store True or False. It can be used as a flag during programming to realize the status of a  component at a particular time interval.

let isAdult: boolean = false; 

Void

This data type is used as a return type for a function that does not return any value. In other words, when no other data type is available, the void type is used. 

function greet(): void {
console.log(“This is a greeting message”);
} 

 

This function prints to the console and has nothing to return. 

It is important to note that we cannot assign a void to a variable as it won't make sense to the compiler. 

Null

This data type is used when a variable has no value assigned to it, or in other words, the value of that variable is undefined.  

let int: number= null; 

 

This way, we have assigned a variable of type number and name int but have not given it any values without introducing any errors in our code. This variable can later be given any value.

int=123; 

 

Alternatively, we can use null to create a variable whose data type and value are undefined. 

let int2: null= null;

Undefined

This data type is a primitive type that denotes all uninitialized variables. 

let int3: number= undefined;
int3=456; 

Any

This data type is a special type that can store all kinds of data.

let value: any= “Raman”; //String value stored
value= 100;              //Value replaced by number data
value= true;             //Value replaced by boolean

 

Any type is beneficial in cases where a function is expected to receive arguments that can be both number or string. 

function test(a:any, b:any): {
console.log(a + b);
} 

 

If the arguments passed are numbers, the output would be the addition of the two numbers; however, If the given arguments are strings, the output would be the concatenation of the two.   

FAQs

  1. What is the difference between Null and Undefined data types?
    When a variable is declared as undefined, it means that the variable has no value or object assigned to it. Null, however, is purposefully used to set an object as undefined for a variable. 
     
  2. What are User-Defined Data Types in Typescript?
    User-Defined Data types are an extension to the Built-in Data types that can be defined by the user and are generally a combination of the primitive types. These include Enumerations (enums), classes, interfaces, arrays, and tuples.

Key Takeaways

From this article, we learned about Typescript's various data types and how their syntaxes are used. We also learned the key differences between the Null and Undefined data types that can be used per the given situation. We looked into how string literals make it easy to concatenate and embed variables and line gaps without much trouble. 

However, this isn't enough, as there is always much more to explore and learn about this vast field of Web Development. To know more about Typescript and its intricacies, check out the articles on Typescript or enroll in our highly curated Web Development course.

To study more about data types, refer to Abstract Data Types in C++.

Live masterclass