Mohammad Umair Khan
Trainer, Mentor, Foo, Bar !
Angular2 is in Typescript/Dart/ES6
A2 is written in TS
It is recommended to use TS when developing in A2
Everything in a class is public if not specified. Everything in a module is private unless export keyword is used.
//typescript
let age:number = 5;
let name: boolean = true;
let address: string = 'neverland';
let numbers: number [] = [1,2,3,4];
let counting: Array<number> [] = [1,2,3,4];
let tuple: [number, string] = [1, 'batman'];
let various: any[] = [1, true, 'wayne', [false, 9]];
enum Color {Red, Green, Blue};
//typescript
let age = 5; //inferred as a number
let name = true; //inferred as a boolean
let address; //inferred as any
let person;
person = 'Wiley ol`fox'; //inferred as any
Typescript can infer types if the variables are not annotated
//typescript
let hero = {
name: 'Johnny Quest'
}; //inferred as an Object with a
//property name being a string
let zero: Object = {
name: 'Major'
}; //will be an Object without any
//property annotation *
*This is where interfaces would kick in
//typescript
//inferred as a type Function
//a, b and return type is any
let add = (a, b) => a + b;
//type Function
//a, b and return type is number
let add: Function = (a: number, b: number) => a + b;
//type Function
//a, b and return type is number
let add: Function = (a: number, b: number): number => a + b;
//typescript
interface IPlayer {
name: string,
age: number,
retired?: boolean
}
let brainLara: IPlayer = {
name: 'Brian Lara',
age: 45,
retired: true
};
//typescript
interface ISum {
(a: number, b: number): number
}
let add: ISum = (a, b) => a + b;
//typescript
interface ICar {
make: number,
manufacturer: string,
color?: string,
running(): boolean
}
//typescript
class Toyota implements ICar {
make: number;
manufacturer: string;
constructor(make: number, manufacturer: string){
this.make = make;
this.manufacturer = manufacturer;
}
running(){
return true;
}
}
By Mohammad Umair Khan