ionic UI(3)TypeScript - handbook
ionicUI(3)TypeScript-handbook
1BasicTypes
boolean-varisDone:boolean=false;
number-varheight:number=6;
string-varname:string=‘carl’;
name=“sillycat”;
Array-varlist:number[]=[1,2,3];
varlist:Array<number>=[1,2,3];
Enum-enumColor{Red,Green,Blue};
varc:Color=Color.Green;
usually,enumbeginfrom0,wecansetthenumberaswell.
enumColor{Red=1,Green=2,Blue=4};
varcolorName:string=Color[2];
alert(colorName);
Thealertmessageis‘Green'
Any-varnotSure:any=4;
notSure=“maybeastringinstead”;
notSure=false;
Void-functionwarnUser():void{
alert(“warningmessage.");
}
2Interfaces
interfaceLabelledValue{
label:string;
}
Thisinterfacerequiresthereisapropertylabelintheparameterobject.
OptionalPropertiesinInterface
interfaceSquareConfig{
color?:string;
width?:number;
}
ClassTypes
interfaceClockInterface{
currentTime:Date;
setTime(d:Date);
}
classClockimplementsClockInterface{
currentTime:Date;
setTime(d:Date){
this.currentTime=d;
}
constructor(h:number,m:number){}
}
ExtendingInterfaces
interfaceShape{
color:string;
}
interfaceSquareextendsShape{
sideLength:number;
}
varsquare=<Square>{};
square.color=“blue”;
square.sideLength=10;
3Classes
InheritanceofClass
classAnimal{
name:string;
contractor(theName:string){this.name=theName;}
move(meters:number=0){
alert(this.name+“canmove“+meters+“m.")
}
}
classSnakeextendsAnimal{
constructor(name:string){super(name);}
move(meters=5){
alert(“slithering….");
super.move(meters);
}
}
Private/Publicmodifiers-publicisalwaysthedefault
Getter/Setter
varpasscode=“secretpasscode”;
classEmployee{
private_fullName:string;
getfullName():string{
returnthis._fullName;
}
setfullName(newName:string){
if(passcode&&passcode==“secretpasscode”){
this._fullName=newName;
}else{
alert(“Unauthorizedupdateofemployee!");
}
}
}
varemployee=newEmployee();
employee.fullName=“CarlLuo”;
if(employee.fullName){
alert(employee.fullName);
}
4Modules
>tsc—outsample.jsValidation.tsLettersOnlyValidator.tsZipCodeValidator.tsTest.ts
5Functions
defaultandoptionalparameters
functionbuildName(firstName:string,lastName?:string){
}
functionbuildName(firstName:string,lastName=“Smith”){
}
6Generics
Moredocumentsinthehandbook,butIthinkIamokthatIonlyreadthefirstfewchapters.
http://www.typescriptlang.org/Handbook
References:
http://www.typescriptlang.org/Handbook