函数

介绍

通过函数可以实现抽象层、模拟类、信息隐藏和模块。typeScript支持类、命名空间和模块。但函数任然是主要定义行为的地方。

函数类型

为函数定义类型

  1. function add(x: number, y: number): number {
  2. return x + y;
  3. }
  4. let myAdd = function(x: number, y: number): number { return x + y; };

书写完整函数类型

  1. let myAdd: (x: number, y: number) => number = function (x: number, y: number): number {
  2. return x + y
  3. };
  4. let myAdd2: (baseValue: number, increment: number) => number = function (x: number, y: number): number {
  5. return x + y
  6. };

推断类型

  1. // myAdd has the full function type
  2. let myAdd = function(x: number, y: number): number { return x + y; };
  3. // The parameters `x` and `y` have the type number
  4. let myAdd: (baseValue: number, increment: number) => number =
  5. function(x, y) { return x + y; };

可选参数与默认参数
可选参数必须在参数的最后

  1. function buildName(firstName: string, lastName?: string) {
  2. if (lastName)
  3. return firstName + " " + lastName;
  4. else
  5. return firstName;
  6. }

有默认初始化值的参数,当用户没有传递参数或者传递的值是undefined时,参数会取得默认值,参数默认值没有位置要求。但是如果带默认值的出现在不带默认值的前面,必须用undefined值来获得默认值。

  1. function buildName(firstName: string, lastName = "Smith") {
  2. // ...
  3. }
  4. function buildName(firstName = "Will", lastName: string) {
  5. return firstName + " " + lastName;
  6. }
  7. let result1 = buildName("Bob"); // error, too few parameters
  8. let result2 = buildName("Bob", "Adams", "Sr."); // error, too many parameters
  9. let result3 = buildName("Bob", "Adams"); // okay and returns "Bob Adams"
  10. let result4 = buildName(undefined, "Adams"); // okay and returns "Will Adams"

剩余参数

  1. function buildName(firstName: string, ...restOfName: string[]) {
  2. return firstName + " " + restOfName.join(" ");
  3. }
  4. let employeeName = buildName("Joseph", "Samuel", "Lucas", "MacKinzie");
  1. function buildName(firstName: string, ...restOfName: string[]) {
  2. return firstName + " " + restOfName.join(" ");
  3. }
  4. let buildNameFun: (fname: string, ...rest: string[]) => string = buildName;

this和箭头函数
基本同JavaScript
重载

方法是为同一个函数提供多个函数类型定义来进行函数重载

  1. let suits = ["hearts", "spades", "clubs", "diamonds"];
  2. function pickCard(x: {suit: string; card: number; }[]): number;
  3. function pickCard(x: number): {suit: string; card: number; };
  4. function pickCard(x): any {
  5. // Check to see if we're working with an object/array
  6. // if so, they gave us the deck and we'll pick the card
  7. if (typeof x == "object") {
  8. let pickedCard = Math.floor(Math.random() * x.length);
  9. return pickedCard;
  10. }
  11. // Otherwise just let them pick the card
  12. else if (typeof x == "number") {
  13. let pickedSuit = Math.floor(x / 13);
  14. return { suit: suits[pickedSuit], card: x % 13 };
  15. }
  16. }
  17. let myDeck = [{ suit: "diamonds", card: 2 }, { suit: "spades", card: 10 }, { suit: "hearts", card: 4 }];
  18. let pickedCard1 = myDeck[pickCard(myDeck)];
  19. alert("card: " + pickedCard1.card + " of " + pickedCard1.suit);
  20. let pickedCard2 = pickCard(15);
  21. alert("card: " + pickedCard2.card + " of " + pickedCard2.suit);