DataTypes in TypeScript.

To start with programming, we should make use of basic units of data. We can’t complete even simple task without defining data. So it’s very important to use data in any programming language. Similarly we have useful data types in TypeScript as well. Let’s see what are they ?

List of DataTypes in TypeScript?

Boolean: Boolean data type represents logical values. It’s basic data type. Boolean data value will be simple tru/false value.

let isExpired: boolean = true;

Number: As we have in JavaScript, Number data type is in TypeScript which is having floating point values. It can be used to represent both integers and decimal values. in addition TypeScript supports binary, octa, decimal, hexdecimal leterals.

let decimalNumber: number = 10;
let hexNumber: number = F08080;
let binaryNumber: number = 0b101011;
let octalNumber: number = 0o432;

String: It’s very important and fundamental data type in any programming language. It represents textual data. We can define this by using “(double quotes) and ‘ (single quotes).

let uesrName: string = "Thirupathi Jarajapu";

TypeScript also supports template strings. It can be called as embedded expressions. And it’s defined by using backtick/backquote.

let userName: string = "Thirupathi J";
let lineOfText: string = `Dear ${userName}, Thanks for contacting us!`;

Array: It’s collection of elements of same data type. We can define array in 2 ways. Please find below are the ways of declaration of array.

let evenNumbers: number[] = [2, 4, 6, 8];
let evenNumbers: Array<number> = [2, 4, 6, 8];

Tuple: It can be used to represet pair of values, which may or many not be of same type. We should follow the same order while declaration and assigning values. Meaning if you declare a Tuple with first element string and second element number then you should follow the same while assigning values. Please find example below.

let user: [string, number]; //1st value should be string and 2nd value should be number
user = ["Thirupathi", 34]; 

Enum: Enumarated data types can be used to give user friendly names to numeric values. Enum will give number indexing to it’s members. Enum indexing starts with 0. If you want to set manually then you can set it.

Enum indexing starts from 0

enum Month {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
let m: Month = Month.Jan;

Enum indexing starts from 1

enum Month {Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};

Any: If you are not sure about the type of data while declaration of varaible, then you can use this data type. So that you can assign any type of data. It may be number, boolean, string, etc….

let anyDataType: any;
anyDataType = 10 //number
anyDataType = "Hello World!" //string
anyDataType = true //boolean

Void: It’s commonly used with functions as a return type as you are not going return anything after done with the sepcific functionality. No use of declaration of type void because you can assign null or undefined to variable when no-value is required.

function myFn(): void{
    console.log("Just wanted to demo void data type!");
}

Null & Undefined: Actually both of them have their own types named Undefined, Null. They are sub types of other data types. You don’t get much confusion on it by reading lot with it. Please find it in simple words.

If you want to represent a variable which is not having value then use NULL.

If you just declare a variable but not assign any values to it then it will be treated as UNDEFINED.

let userName: unefined = undefined; 
let userName: null = null

Above declaration is not allowed if –strictNullChecks flag is set. So variable should be declared as any data type.

Conclusion:

Just wanted to explain most useful data types in TypeScript. You may find few other data types but above are mostly used data types.

Hope it helps ! Thanks for reading this !!!