TypeScript Language Features

Optional Static Typed

TypeScript is a typed superset of JavaScript. However it is optional. 

So to be specific , TypeScript has optional static type annotation, which will in turn transform JavaScript into a strongly typed programming language.

This typing is applicable to variables, functions and properties. 

The objective is to provide better verification and assistance (such as IntelliSense) during software development.

Moreover Strong Typing will allow programmers to express his intentions of using that code which will be a useful information for the whole development team.

All the type checking happens ONLY at compile time and no runtime overheads exist during program execution. These constraints will get erased during compilation.

Optional Static Type Notation

TypeScript is good at automatically detecting types.


var counter = 0;  // it will be treated as number
var counter;  // it will be treated as any


There are cases where TypeScript cannot determine the type automatically. In TypeScript type declaration happens after the variable name. This is to reinforce the idea that declaration is optional.


var counter: number; // define the type as number
var counter: number = 0; // define + initialization.

Type Inference is a process where the where the types are identified automatically. When it cannot be determined the “any” type will be used.

Basic Data Types

Boolean : This type can take two values  – true or false


var isDuplicate: boolean = true;
var isFlagged: boolean;

Number: All numbers in TypeScript are floating point values.


var height: number = 10;
var weight: number;

String:  To represent a text in TypeScript. It must be enclosed in Single or Double Quotation marks

Double Quotation marks can be contained in string enclosed in Single Quotation marks where as Single Quotation marks can be contained in String enclosed in Double Quotation marks.


var name: string = "TechThali";
var name = 'Thali';

Array: To work with array of values as in JavaScript. Array can be represented in two ways. [1] Using Square bracket and [2] Using generic array type


var list: number[] = [1,2,3,4,5,6];
var list:Array = [1,2,3,4,5,6];

Enum: To give friendly name to the set of numeric values.
By default it numbers members starting from Zero (0). However it can be manually changed by assigning value to each of these members.


enum Color {Red, Green, Blue}
var c: Color = Color.Blue; 
console.log(c); // will print 2


enum Color {Red=2, Green, Blue} // here Green will get Values 3 and 4
enum NewColor {Red, Green=11, Blue} // here Red will get Zero, Green will get 11 and Blue 12
var c: Color = Color.Blue; 
var d: Color = NewColor .Blue; 
console.log(c); // will print 2
console.log(d); // will print 12

Any: It is used to represent any JavaScript value. Minimal type checking is performed for operations on “any” value.


var notSure: any = 5;
console.log(notSure > 4); // will return true
notSure = "maybe a string instead";
console.log(notSure.length); // will return 22
notSure = false; // okay, definitely a boolean
console.log(notSure==true); // will return false

any datatype is a powerful way to work with existing JavaScript. it will allow you to opt in and out during compilation.
An Array of any can hold values of different types


var a: Array = [10, "Hello", false];
console.log(a[0] > 2); // will return true
console.log(a[1].length); // will return 5
console.log(a[2] == false); // will return true

void: It is the opposite of any. It shows the absence of having any type at all.
This will be often used in a function which has no return value.


function helloWorld(): void {
    alert("Welcome to the World of TypeScript");

Undefined and null

JavaScript primitive types also include undefined and null
is a value given to a variable which is defined but NOT initialized.
Value null is a literal which can be assigned to a variable to represent NO Value


var object;
console.log(object); // will print undefined
console.log(typeof object);// will print undefined


var object = null;
console.log(object); // will print null
console.log(typeof object);// will print object

Please note null and undefined are NOT data types


var object: null; // ERROR.. Invalid usage as null is not a data type
var object: undefined; // ERROR. Invalid usage as undefined is not a data type

Posted in: Programming

Leave a Comment