TypeScript DataTypes
By: Anji
Tags: Angular, Typescript, JavaScript

Similar to other programming languages we can assign the datatypes to the variables and return types for the functions which makes the system more reliable and easily maintainable.

Before delving into the TypeScript types let us understand the problem that the TypeScript is solving when dealing with enterprise applications that are building using JavaScript.

JavaScript is a dynamically typed language. This means JavaScript does not know what type a variable is until it is actually instantiated at run-time. It would be very difficult to handle large applications without having type checking as it leads to issues.

DataTypes

number

string

boolean

enum

void

null

undefined

any

never

Array

tuple

User-defined Types (Classes, Interfaces, etc.)

Number Type

It represents numeric values including decimals, hexadecimal, binary, and octal literals. But to use binary and octal literals, you must use a TypeScript version which follows ECMAScript 2015 or higher.

To declare the Number data type for a variable, you need to use the number keyword.

const intValue: number = 100;

let decimalValue: number = 10;

let hexaDecimalValue: number = 0xf10b;

let binaryValue: number = 0b110100;

let octalValue: number = 0o410;

Boolean Type

It represents true/false values. To declare the Boolean datatype for a variable, you need to use the boolean keyword

let hasAdminRole:Boolean = false

String Type

It represents the string values. To declare the String data type for a variable, you need to use the string keyword.

let userName:string = "admin";

Enum Type

It represents the numerical constants. To declare the Enum datatype for a variable, you need to use the enum keyword.

enum CardTypes {

Debit,

Credit,

Virtual

}

let card: CardTypes = CardTypes.Debit;

By default, the enum values start from 0 (zero), but you can also set it by manually entering the value as mentioned below.

enum CardTypes { Debit = 1, Credit, Virtual }

enum CardTypes { Debit = 1, Credit = 3, Virtual = 5 }

Void datatype

void datatype is used to functions that does not return any value.

showNotification(): void {

alert('hello world');

}

Null Type

It represents the null value. To declare the null datatype for a variable, you need to use the null keyword. As null is a subtype of all other types, you can assign the null value to all types.

let nullValue: null = null;

let numericValue: number = null;

let booleanValue: boolean = null;

let stringValue: string = null;

Undefined Type

It represents the undefined value. To declare the undefined data type for a variable, you need to use the undefined keyword. As undefined is a subtype of all other types, you can assign the undefined value to all types.

let undefinedValue: undefined = undefined;

let numericValue: number = undefined;

let booleanValue: boolean = undefined;

let stringValue: string = undefined;

Any Type

When the developer is unsure of the data type of value (third-party library/service), it is recommended to use any type. To declare any data type for a variable, you need to use any keyword. This is also useful when you are declaring an array which has a mixed data type.

let anyValue: any = 100;

anyValue = "string value";

anyValue = true;

let arrayList: any[] = [ "String Value",100, true];

Never Type

The never type is used when you are sure that something is never going to occur. For example, you write a function which will not return to its endpoint or always throws an exception.

function throwError(errorMsg: string): never {

throw new Error(errorMsg);

}

function batchProcessing(): never {

while (true) {

console.log('call REST API');

}

}

Array Types

Arrays can be defined in the Typescript in 2 ways.

let userIds: number[] = [80, 85, 75]; let orderIds: Array<number> = [50000, 50001, 50001];

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 the same.

let employee: [string, number, boolean] = ["sam", 2019, true];

let empName:string = employee[0];

let joinYear:number = employee[1];

Union Type

TypeScript allows us to use more than one data type for a variable or a function parameter.

Syntax: (type1 | type2 |.. | typeN)

let empId: (string | number); empId = 123; // OK

empId = "TE123"; // OK

empId = false; // Compiler Error

As we are talking about data types, let us talk about the type inference in typescript.

Type Inference meaning “types of variables will be inferred when there is no explicit information available in the form of type annotations.”

Types are inferred by TypeScript compiler when:

Variables are initialized

Default values are assigned for the parameters

Function return types are determined based on the input parameters