Types

We all know that Java Script has only one type called var. All other variables which are not declared as var are of global scope. One more interesting thing is JavaScript has function scope whereas other traditional languages has block scope. Now we are going to see the rich set of types available in Type Script. There are three main types in TypeScript.

Basic Types

  • number
  • string
  • boolean

All the values which TypeScript process will fall under one of these three types. Let us see few examples of these.

var name: string = "Naren Arya";
var age: number = 24;
var score: number = 8.3;
var isMarried: boolean = false;

If we see the above statements a variable declaration in TypeScript is happening using types attached after the variable declaration.

So if we try to store a string in integer type variable, TypeScript throws an error warning saying that we cannot store string in a number;

age = "Twenty-Three";

error: cannot store string type in number

Types are the heart of Type Script. Very exciting type checks imposition will come soon in the next chapters.

More Types

By extending these basic types we can have more advanced types like arrays, tuples, enums in the TypeScript.

var myNumberArray: number[] = [1,2,3,4,5];
var myStringArray: string[] = ["one","two","three"];

If we try to assign a different value to the particular type we will see an error.

myStringArray[2] = 5;
error: Cannot store number type in string

But, how we can relax this typing whenever required. Here comes the any key word.

'any' type

any keyword is used to declare a variable which can hold any type. Normally we see such thing in dynamic programming languages. A variable can hold many types in it.

var noIdea: any = 22;
noIdea = false; // can hold boolean type
noIdea = "Second Guess"; // can even hold string type

The classic enumerate type

enum type is available in the Type Script to use informative names instead of magic numbers in our code.

// Declaring Enumerate in type script
enum direction = {EAST, WEST, NORTH, SOUTH};

// Here EAST will have a value 0
var east_direction = direaction.EAST;

// We can also get direction from value
var south_direction = direction[4];

Good old Void

If our program demands us to create a variable which only holds either undefined or null then we should declare that variable as a void type. It means void is complement to any type.

var noValue: void;
noValue = undefined; //acceptable
noValue = null; //acceptable
noValue = 5; //typescript throws error

With this, We can also make sure that JavaScript function should return nothing.

Tuples

Another main drawback of JavaScript is the lack of tuple types similar to Python and Ruby. Type Script fills this gap by allowing us to create tuple like structures.

var student: [number, string, number];
student = [20317, "Elon Musk", 12]; // correct!
student = [23017, 12, "Elon Musk"]; // Error invalid tuple order

We can access values of a tuple similar to an array. For above declared tuple

student = [20317, "Elon Musk", 12];
console.log(student[0]); // returns 20317
console.log(student[6]); //returns undefined

When we try to access not existed array index, Type Script returns undefined similar to the JavaScript.

Built in Type Inference

Type Script has built in type inference through which it assumes types of variables according to the assignment of value at time of declaration.

var name = "Naren"; // No need to mention as string
var name: string = "Naren"; // Verbose way
var arr = [1,2,3,4,5] // type is string[]

In my opinion explicitly mention of type will always helps the new developers who works on legacy code.

Popularity of Type Script post ES6

The popularity of Type Script is sky rocketed when libraries like Angular JS, Native JS and Aurelia adopted it for their code base. This is due to the new language improvements from ES6. According to GitHub survey between 2015-16 developers are looking at Type Script.

The boom of Type Script in 2016 is like hilarious increase in Spanish income tax rates, courtesy: XKCD