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;
}
}
//typescript
function identity<T>(arg: T): T {
return arg;
}
//typescript
interface GenericIdentityFn<T> {
(arg: T): T;
}
//typescript
class Greeter<T> {
greeting: T;
constructor(message: T) {
this.greeting = message;
}
greet() {
return this.greeting;
}
}
function ClassDecorator(
target: Function // The class the decorator is declared on
) {
console.log("ClassDecorator called on: ", target);
}
@ClassDecorator
class ClassDecoratorExample {
}
function PropertyDecorator(
target: Object, // The prototype of the class
propertyKey: string | symbol // The name of the property
) {
console.log("PropertyDecorator called on: ", target, propertyKey);
}
class PropertyDecoratorExample {
@PropertyDecorator
name: string;
}
function MethodDecorator(
target: Object, // The prototype of the class
propertyKey: string, // The name of the method
descriptor: TypedPropertyDescriptor<any>
) {
console.log("MethodDecorator called on: ", target, propertyKey, descriptor);
}
class MethodDecoratorExample {
@MethodDecorator
method() {
}
}
function StaticMethodDecorator(
target: Function, // the function itself and not the prototype
propertyKey: string | symbol, // The name of the static method
descriptor: TypedPropertyDescriptor<any>
) {
console.log("StaticMethodDecorator called on: ", target, propertyKey, descriptor);
}
class StaticMethodDecoratorExample {
@StaticMethodDecorator
static staticMethod() {
}
}
function ParameterDecorator(
target: Function, // The prototype of the class
propertyKey: string | symbol, // The name of the method
parameterIndex: number // The index of parameter in the list of the function's parameters
) {
console.log("ParameterDecorator called on: ", target, propertyKey, parameterIndex);
}
class ParameterDecoratorExample {
method(@ParameterDecorator param1: string, @ParameterDecorator param2: number) {
}
}