Lastly, void is used in the special case of a function returning nothing.
functionbigHorribleAlert():void {alert('I'm a little annoying box!');}
Functions
Functions are first class citizens, support the lambda 'fat arrow' syntax and use type inference.
The following are equivalent, the same signature will be inferred by the compiler, and same JavaScript will be emitted
Regular function
// Function Declaration
function f1(i: number): number {
return i * i;
}
// Function Expression let f1 = function (i: number): number { return i * i; }
Return type inferred
let f2 = function (i: number) {
return i * i;
}
'Fat arrow' syntax
letf3= (i:number):number=> {return i * i;}
'Fat arrow' syntax with return type inferred
letf4= (i:number) => { return i * i; }
'Fat arrow' syntax with return type inferred, braceless means no return keyword needed.
letf5= (i:number) => i * i;
var, let, and const
Declaring a variable using the above keywords:
Both type and initial value.
var width:number=100;let height:number=200;constkey:string='abc123';
Without type, but with an initial value.
var width =100;let height =200;constkey='abc123';
Only the type.
var width:number;let height:number;// const does not support without initial value
Without type and initial value.
var width;let height;// const does not support without initial value
functionsomeFn() { if (true) { // defined locally// its scope ends where curly braces endsvar local =1000;console.log(local); //ok }console.log(local); //okfunctionnested() { console.log(local); //ok }}console.log(local); //error
let and const are Block Scoped
functionsomeFn() { if (true) { // defined locally// its scope ends where curly braces endslet local =1000;console.log(local); // ok }console.log(local); //errorfunctionnested() { console.log(local); //error }}console.log(local); //error
Interfaces
Interfaces are structural, anything that has the properties is compliant with the interface.
interfacePerson { name:string;// Optional properties, marked with a '?' age?:number;// And of course functionsmove():void;}
Object that implements the Person interface can be treated as a Person since it has the name and move properties
Only the parameters' types are important, names are not important.
let mySearch:SearchFunc;mySearch=function (src:string, sub:string) {returnsrc.search(sub) !=-1;}
Classes
Class members are public by default
classPoint {// Properties x:number;/* Constructor - the public/private keywords * in this context will generate the boiler * plate code for the property and the * initialization in the constructor. * * In this example, * 'y' will be defined just like 'x' is, * but with less code * * Default values are also supported */constructor(x:number,public y:number=0) {this.x = x; }// Functionsdist() {returnMath.sqrt(this.x *this.x +this.y *this.y); }// Static membersstatic origin =newPoint(0,0);}
let p1 =newPoint(10,20);let p2 =newPoint(25); //y will be 0
Classes can be explicitly marked as implementing an interface. Any missing properties will then cause an error at compile-time.
classPoint3DextendsPoint {constructor(x:number, y:number,public z:number=0) {// Explicit call to the super class constructor // is mandatorysuper(x, y); }// Overridedist() {let d =super.dist();returnMath.sqrt(d() *d() +this.z *this.z); }}
let tuple =pairToTuple({ item1:'hello', item2:'world'});
Template strings
Template Strings: strings that use backticks (`).
String Interpolation with Template Strings
let name ='Tyrone';let greeting =`Hi ${name}, how are you?`
Multiline Strings with Template Strings
let multiline =`This is an exampleof a multiline string`;
Iterators
for..of statement. Iterate over the list of values on the object being iterated.
let arrayOfAnyType = [1, 'string', false];
for (const val of arrayOfAnyType) { console.log(val); // 1, 'string', false }
let list = [4, 5, 6];
for (const i of list) {
console.log(i); // 4, 5, 6
}
let arrayOfAnyType = [1, 'string', false];
for (const val of arrayOfAnyType) {
console.log(val); // 1, 'string', false
}
let list = [4, 5, 6];
for (const i of list) { console.log(i); // 4, 5, 6 }
for..in statement. Iterate over the list of keys on the object being iterated.
for (const i in list) {
console.log(i); // 0, 1, 2
}