Basic Data Types in TypeScript?
Tags: typescript, angular, datatype
Published: 2019-11-25 16:30:52

Common data types in TypeScript are
Number, String, Boolean, Enum, Void, Null, Undefined, Any, Never, Array and tuple.

Syntax for typescript DataType

var <--variableName--> : <--dataType--> = <--value-->;

Number Types
In TypeScript, numbers are floating point values that have a type of number. 

Example:1
let decimalValue: number = 10;
let hexaDecimalValue: number = 0xf10b;
let binaryValue: number = 0b110100;
let octalValue: number = 0o410;


String Types
When you want to use textual data, string types are used and get denoted by the keyword string. Like JavaScript, TypeScript also uses double quotes (") and single quotes (') to surround the string value.

Example:1
let firstName: string = "bibi"; // using double quotes
let lastName: string = 'umme';  // using single quotes
My name is: ${firstName} ${lastName}`; // if you want to pass data dynamically this is called templated string


Boolean Types
boolean keyword is used in this data types as same as javascript

Example:1
let isActive: boolean = true;
let isOn: boolean = false;



Enum Types
Enumerated data types (enums) are a set of numeric values with more friendly names.The variables of enumerated data types are declared with the keyword enum.

Example:1
enum Direction { Up, Down, Left, Right }

By default, the enum values start from 0 (zero), but you can also set it by manually entering the value of its members.

enum Direction { Up = 1, Down = 3, Left = 5 }


Example:2
enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
var day: Days = Days.Monday; // print 1


Void Types
This type of data types are used in functions that do not return any value. 

Example:1
function showData(): void { ... }.


Null Types
Null keyword is used and can assign only null value to it. As null is a subtype of all other types, you can assign it to a number or a boolean value.

Example:1
let nullExample: null = null;
let numericValue: number = null;


Undefined Types
Undefined data type use to store the value undefined to it. undefined can be assign it to a number or a boolean value. 

Example:1
let undefinedValue: undefined = undefined;
let numericValue: number = undefined;



Any Types
when  unsure of the data type of a value, due to its dynamic content, you can use the keyword any to declare any data type. This is often useful when you are seeking input from use

Example:1
let dynamicValue: any = "Umme habiba";
dynamicValue = 120;
dynamicValue = 0b11100101;
dynamicValue = false;
let dynamicList: any[] = [ "Umme", "Software Developer", 25, true];




Never Types
The never type represents the data type of values that never occur.

Example:1
keepProcessing() function is always executing and never reaches an end point because the while loop never ends.

function keepProcessing(): never { 
            while (true) { 
         console.log('I always does something and never ends.')
     }
}


Array Types
There are two ways of declaring Array in TypeScript.
    1: using []
    2: using Array<data type>


Example:
let marks: number[] = [80, 85, 75];
let marks: Array<number> = [80, 85, 75];
 var name: Array<string> = ["Sheo", "Narayan", "Dutta"];



Tuple Types
A tuple is a data type that allows you to create an array where the type of a fixed number of elements are known but need not be same. 

Example:
var student: [number, string] = [1, "umme"];

var employee: [number, string, boolean] = [1, "bibi", true];