Technical Lead
Synerzip Softech India PVT LTD
@pankajparkar
Background
Introduction to Typescript
What is Typescript
Why should I use Typescript?
How it works?
How to use it?
Installation, setup and configuration.
Typescript Fundamentals
ES3
ES5
ES6
ES7
ES8
ES
Next
.
.
.
1997
2010
2015
2016
2017
Typescript is a typed superset of Javascript that compiles to plain Javascript. Any Browser.
Any host. Any OS. Open Source.
Static Typing
Javascript has dynamic typings.
Good
Bad
.Net
Java
Ruby
PHP
- The TypeScript type system
- Your JavaScript is TypeScript
- Types inference
- Types are structural using classes/interface
- Type errors do not prevent JavaScript emit
- Types can be ambient
var name: Type = 'Value';
var myVariable = '123';
var foo = 123;
foo = '456'; // Error: cannot assign a `string` to a `number`
// Still it compiles to below
var foo = 123;
foo = '456';
Typescript file(.ts)
Typescript Compiler
(uses tsconfig.json)
Javascript file(.js)
Other way is, you could go on typescript playground
http://www.typescriptlang.org/play/
npm install -g typescript
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
let greeter = new Greeter("world");
class Animal {
name: string;
constructor(theName: string) { this.name = theName; }
move(distanceInMeters: number = 0) {
console.log(`${this.name} moved ${distanceInMeters}m.`);
}
}
class Snake extends Animal {
constructor(name: string) { super(name); }
move(distanceInMeters = 5) {
super.move(distanceInMeters);
}
}
class Horse extends Animal {
constructor(name: string) { super(name); }
move(distanceInMeters = 45) {
super.move(distanceInMeters);
}
}
let add = (x: number, y: number): number => {
return x + y;
};
add(1, 2);
By using Interface you can focus type-checking on the shape of object.
interface SquareConfig {
color?: string;
width?: number;
}
let newSquare: SquareConfig = {
color: "white",
area: 100
};
Flexible Typing
function loggingIdentity<T>(arg: T): T {
console.log(typeof T)
console.log(arg.length); // Error: T doesn't have .length
return arg;
}
Flexible Typing
enum Language {
Java,
CSharp,
Javascript
}
// Sample usage
var language = Language.Java;
// Error `string` is not assignable to type `Language`
// Safety
language = "not a member of card suit";