Saturday, June 27, 2020

Basic types in TypeScript

Basic Types in TypeScript


TypeScript is a strongly typed language, it supports different types. The validity of the given values is checked by the Type System. The type of the variable is declared at the start and this type will be maintained through the execution otherwise it will result in a compile-time error.

We can work with simple data types, such as numbers, strings, objects, Booleans, etc.

All of the JavaScript data types are supported by TypeScript along with the enumeration data type.


Boolean


The boolean is a basic datatype holding true/false value, for example

 

let finished: boolean = false;


Number


Similar to JavaScript, the numbers in TypeScript are floating-point values. These floating-point numbers get the type number. In addition to hexadecimal and decimal literals, It also supports binary and octal literals described in ECMAScript 2015.
let a: number = 16;
let b: number = 0xf01d;
let c: number = 0b1110;
let d: number = 0o644;


String 


The string data types are used to represent the text data. Similar to JavaScript strings, TypeScript also uses double quotes (") or single quotes (') to enclose the string data.

let brand: string = "Honda";


We can use template strings, those can span multiple lines with embedded expressions. Template strings are enclosed by the backtick/backquote (`) character, and the embedded expressions are represented in the form ${ expression }. For example,

let fName: string = `James Bond`;
let age: number = 29;
let sentence: string = `I am Bond, ${ fullName }.
i am ${ age + 1 } years old next week.`;

Arrays 


Similar to JavaScript arrays, we can declare and work with arrays containing values. The types of array can be declared in one of two ways. First, by using the type of the items followed by [] to represent an array containing that element type.

let nums: number[] = [10, 20, 30, 40];
The second way is by using Generic types.

let nums: Array<number> = [10, 20, 30, 40];

Tuple 


Tuple enables to represent an array with a fixed number of items whose types are known, but not required to be the same. For example, to represent a value as a pair of a string and a number, we can write,

// Declare a tuple type
let t: [string, number];
// Initialize it
t = ["hello", 10]; // OK
// Initialize it incorrectly
t = [10, "hello"]; // Error

Enum 


Enums are a useful addition to the standard set of data types available in JavaScript. The purpose of the enums is the same as in languages like C#, as they provide a way of giving more friendly names to sets of numeric values.

enum Brand {Google = 1, Apple, Samsung}
let brandName: string = Brand[2];

console.log(brandName); // writes 'Apple' as its value is 2 above

Any


Sometimes, it possible that the type of the variable is not known, and the values are generated by any dynamic content like some random user or third party library. In such a situation the type of the variable can be declared as ‘any’.

const isFinished: any = 4;
isFinished = "Not yet"; // String
isFinished = True; // boolean

void 


The void is generally used as the return type of a function which does not return a value. A void type represents that variable does not have a type. The behavior of the void is just opposite to ‘any’.

function showMessage(): void {
    console.log("Welcome Message");
}

It is not fruitful to declare a variable of type void because they can only be assigned null or undefined.

const unfruitful: void = undefined;
unfruitful = null;


Null and Undefined


undefined and null are types names actually have their own type names Null and undefined respectively. However, they are very useful themselves like void,

const v1: undefined = undefined;
const v2: null = null;


Never


The never type are used as the type of values that never occur. For example, never is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns.

A variable can also obtain the type never when narrowed by any type guards that can never be true. The never type is a subtype of, and assignable to, every type; however, no type is a subtype of, or assignable to, never (except never itself). In fact, ‘any’ cannot be assigned to a never.


Object 


The user-defined types can be represented by the object (types excluding number, Boolean, string, symbol, bigint, null, undefined).