Sunday, July 15, 2018

Typescript Datatypes - tutorials with examples

Datatypes Introduction

typescript data types - Strings, Enums

 Once you read Typescript Installion and Hello world program article, You got a clear understanding on how to install typescript and write hello world program. In this article, I will walk through the basic data types of typescript language. Typescript has all data types that support in javascript supported data types in typescript are void, Null, undefined, boolean, string, Number, array and tuple data types, Enum, Any and Never data types.

Basic data type syntax 

Variables can be declared using the below syntax

[let/var/const] variablename: [datatype];

variable names are valid names datatypes are either builtin type or custom data types like object.

  Boolean data type

This is a basic data type which has only true or false values. The same also available in javascript as well as other programming languages like java Boolean types are basically used in if-else and while logical flows.

let isChecked: boolean;  // valid
let isFlag:boolean=true; // valid
let isValid1:boolean=213; // Not valid and gives compile time error
let isValid2:boolean='test'; // Not valid and gives compile time error

isValid1 and isValid2 are not valid values. and gives Type '213' is not assignable to type 'boolean' compile time error. 

Number data type 

Typescript has no integer, long but it has number data type which is by default floating number. We can represent integer and floating values in number data type.

let n1: number = 123; // valid
let n2: number = 0o987; // Octal
let n3: number = 0xbac01; // Hexadecimal
let n4: number = "string"; // Not Valid. i.e Error
let n5: number = null; //  Valid
let n6: number = undefined; // Valid

String data type

The string is a common data type in any programming language. The string represents a group of character saved under a variable of String type. The string can be declared using double quotes or single quotes. Please see String Complete examples

let s1: string = undefined; // valid
let s2: string = null; // valid
let s3 : string = ""; // valid
let s4: string = "building"; // valid
let s5: string = 12; //  Not valid

Arrays data type Object

Arrays are one of the basic and important data type available in javascript and typescript. Arrays group multiple values with size not known. Arrays are one of the data type in typescript collection. Arrays can be created in multiple ways like Generic Arrays or typed Arrays

let numerArrays: number[] = [11, 2, 10]; // Number Generic  Arrays
let stringArrays: string[] = ["jan", "Feb", "Mar","Apr"]; // valid array
let numberArrays: number[] = [1, "Jan", "Feb"]; // Not valid array
let months: Array = ['January', 'Febrary', 'March']; // Typed array declaration

Tuple Datatype 

Tuple data type is used to create a fixed group different data type values. Data types can be different

let dataMix: [number, string];
dataMix = [2000,'Jan salary'];

Enum Datatype 

 Enum is to represent the collection values stored under one name called Enums of items. It is the Enumeration of items.

enum Diagram {Recntagle, Circle, Square};
let myFirstDiagram: Diagram = Diagram.Recntagle;
please see Detailed Enum tutorials with all examples

Void Datatype 

void means nothing. The void is used in return type of methods/functions. Void in method means doesn't return anything.

function testVoidMethod(): void {
    console.log("this is to test Void data type")

Undefined Datatype

 In javascript, if any variable is not initialized with any value, by default it initiates with undefined. We can also assign an undefined value

let numberUndefined: number = undefined; // valid
let voidUndefined: void = undefined; // Valid

Any and Never data type

any data type is used to hold the values of different data types

let stringValue: any = "anyvalue";
let booleanValue: any = true;
let arrayValues: any[] = ["anyvalue",  true, null, 986,];
let numberValue: any = 54;

Never data type is used to specify the data/function flow will never run. It can be used with function return type as well as for variables

function infiniteMethod(): never {
    while (true) {
    return 'this line never runes ';
Please check other articles on typescript tutorial series

Typescript Tutorials and Examples

Install and Hello World Example
Datatypes Tutorials and Examples 
Enum Tutorials and Examples 
String Tutorials and Examples 


Note: Only a member of this blog may post a comment.