JS Logo

TypeScript

One of the best thing from microsoft

by Vineesh N P / @vineeshvalsalan

What is TypeScript?

Superset of JS

Compiled to JS by tsc

Works on any OS, browser, host

It's open source

Why TypeScript?

Maintaining big JS projects

Large scale JS Application Architechure

Classes, Inheritance, Interfaces

Installation?

npm install -g typescript

create helloworld.ts

tsc helloworld.ts

your js file associated with ts is generated

Lets take a look at TS homepage

and see an example

How to typescript?

							
var name = 'Lambert';
name = 10 //possible in JS

var name = 'Sijin';
name = 1100 //cannot convert string to number
							
          

Declaration

							
var name:string;

var age:number;

var cars:Array<string> = []

cars[0] = 'toyota'

cars[1] = 9712 // error
							

Arguments

							
function setName(name:string, age:number){
	console.log('Age of ' + ' is '+age)
}

setName('Akhil', 15)

setName('Sreenadh', 18)

function getName():string{
	retrun name;
}
							

Functions

there are several ways of decleration

Lets explore it as we dive through it

							
var getName = ():string =>{
	return 'Aboobacker';
}
							

Objects and Interfaces

Objects we have saw on ES6

Lets see in whats in TS

Interfaces

How can we pass an object as an argument.

								
interface IPerson{
	firstName : string;
	lastName : string;
	sayHi? : () => void; //? says its function is optional
};
function recivePerson(person:IPerson){
	person.sayHi();
}
								

Objects

Create an object in TS

									
class Person{
	firstName:string;
	lastName:string;
	constructor(fName:string, lName: string){
		this.firstName = fName;
		this.lastName = lName;
	}

	sayHi():string	{
		return "My Name is "+this.firstName+" "+this.lastName;
	}
}
									

Modules

Dont use global vars

Protect js code

your code gets ruined when you try to add 3rd party libs

										
export class Person{
...
	}
										

Apart from this

function overloading

Inheritance

Modules

Decorators

blah blah blah...

Tired?