function extension function in TS full function type
JS defines functions function
Add (x, y) {return
x + y;
}
TS to define the complete function let
add: (X:number, Y:number) => number
= function (X:numer, Y:number): number {return
x + y ;
}
The parameters in the function are not to be consistent with the arguments in the type declaration, so long as the parameter types are consistent,
function (Basevalue:number, increment:number) {return
x + y;
}
//Of course, you can also use the type inference function in TS, do not need to declare the parameter type in the function, TS will automatically check according to the context, such as: Let
add: (X:number, Y:number) => number
= function (x,y) {return x + y;}
This is the benefit of defining functions in TS, always let you know your original idea, don't let you deviate from the original track,
//Time to restrain you, do not let you make mistakes, haha
Optional parameters and default parameters
In JS, each parameter is optional, can not pass, no biography, the value is undefined. But in TS, it's not that comfortable. The definition of the number of function parameters, you have to preach how much, can not be more or less, otherwise, it will error
function Buildname (firstname:string, lastname:stirng) {return
FirstName + "" + LastName;
}
Let RESULT1 = Buildname ("Bob"); Error let
result2 = Buildname ("Bob", "Yu");//right let
result3 = Bulidname ("Bob", "Yu", "Sr.");//error
// notation for optional parameters in TS
/And also, it is important to note that optional parameters must be followed by the required parameters, in order to not disorderly
function buildname (firstname:string, lastName?: string) {
if (lastName) {return
FirstName + "" + LastName;
} else{return
firstName
}
}
The default parameter representation method in TS
//In TS when the user does not pass this parameter or the value passed is undefined, you can set this parameter to the default parameter, that is, the parameter of the default initialization value, if you wish.
function Buildname (firstname:string, lastname:string = "Smith") {return
FirstName + "" + LastName;
}
remaining Parameters
The necessary parameters, the default parameters and optional parameters have one thing in common: they represent one parameter. When you want to manipulate multiple parameters at the same time, or you do not know how many parameters will be passed in. In JS, you can use arguments object access, of course, in the TS can also be done. In TS, we collect all the parameters into an array.
function Buildname (firstname:stirng, ... restofname:string[]) {return
FirstName + "" + Restofname.join ("");
}
This and arrow functions
This is good, but generally good things are not to be asked for, haha
JS Let deck = {suits: ["Hearts", "spades", "clubs", "Diamonds"], Cards:array (), Createcardpicker:fu
Nction () {return function () {Let Pickedcard = Math.floor (Math.random () * 52);
Let Pickedsuit = Math.floor (PICKEDCARD/13);
return {Suit:this.suits[pickedsuit], card:pickedcard% 13};
}
}
};
Let Cardpicker = Deck.createcardpicker ();
Let Pickedcard = Cardpicker ();
Console.log ("card:" + Pickedcard.card + "of" + pickedcard.suit); If you try to run the above program, you will find that there is no pop-up dialog box, but the error.
Because this in the function returned by Createcardpicker is set to window instead of deck object. To solve this problem, we can bind the correct this when the function is returned. The arrow function can save the this value at the time the function was created, not the value at the time of the call.
This + TS is a perfect match.
TS Rewrite the above example interface card {suit:string;
Card:number;
} interface Deck {suits:string[];
Cards:number[];
Createcardpicker (This:deck): () => card; Let Deck:deck = {suits: ["Hearts", "spades", "clubs", "Diamonds"], Card:array (), Createcardpicker:fu
Nction (This:deck) { Return () => {Let Pickedcard = Math.floor (Math.random () * 52);
Let Pickedsuit = Math.floor (PICKEDCARD/13);
return {Suit:this.suits[pickedsuit], card:pickedcard% 13};
}} Let Cardpicker = Deck.createcardpicker ();
Let Pickedcard = Cardpicker (); Console.log ("card:" + Pickedcard.card + "of" + pickedcard.suit);Overload
JS is not overloaded, but welcome to congratulate, TS can be used in the overload
Let suits = ["Hearts", "spades", "clubs", "Diamonds"]; function Pickcard (x: {suit:string; card:number;}
[]): number;
function Pickcard (x:number): {suit:string; Card:number};
function Pickcard (x): any {if (typeof x = = "Object") {Let Pickedcard = Math.floor (Math.random () * x.length);
return pickedcard;
else if (typeof x = = "Number") {Let pickedsuit = Math.floor (X/13);
return {Suit:suits[pickedsuit], card:x% 13}; }//object Let Mydeck = [{suit: "Diamonds", card:2}, {suit: "Spades", card:10}, {suit: "Hearts", card:4}]
;
Let PickedCard1 = Mydeck[pickcard (Mydeck)];
Alert ("card:" + Pickedcard1.card + "of" + pickedcard1.suit);
Number Let PickedCard2 = Pickcard (15);
Alert ("card:" + Pickedcard2.card + "of" + pickedcard2.suit);
Note: function Pickcard (x): Any is not part of the overloaded list, so there are only two overloads: one is the receiving object and the other is the receiving number. In order for the compiler to choose the correct type of check, it is similar to the processing process in JavaScript. Find the overloaded list first, try using the first overload definition, and use this if it matches. Therefore, when defining overloads, be sure to put the most precise definitionThe front.