Static Typing in TypeScript

TypeScript bring type checking into JavaScrit which is a non-strict in its type.

TypeScript’s basic types are the same ones we’ve been using implicitly when we write “normal” JavaScript code: strings, numbers, booleans, etc.

We still use var to define a variable
var name:string;

We can define the return type and the type of the arguments

function greeting(name: string): string {  
    return "Hello " + name; 
}

Built-in types

String

var name: string = 'Reddy';

Number

A number is any type of numeric value. In TypeScript, all numbers are represented as floating point. The type for numbers is number:

var age: number = 12

Boolean

var male: boolean = true

Array

We specify the type of the items in the array with either the Array or type[] notations:

var jobs: Array<string> = ['IBM', 'Microsoft', 'Google'];  
var jobs: string[] = ['Apple', 'Dell', 'HP'];  
var jobs: Array<number> = [1, 2, 3];  
var jobs: number[] = [4, 5, 6];  

Any

any is the default type if we omit typing for a given variable. Having a variable of type any allows it to receive any kind of value:

var something: any = 'as string';  
something = 1; something = [1, 2, 3];  

Void

Using void means there’s no type expected. This is usually in functions with no return value:

function setName(name: string): void {  
    this.name = name; 
}