TypeScript---函数Function

在函数和返回值类型之前使用( =>)符号(变量是一个函数时 变量定义时 定义的返回值用=>表示)

// 完整 类型定义写法
let myAdd: (x: number, y: number) => number =
    function(x: number, y: number): number { return x + y; };
// 类型推断,可以省略后面的参数定义
    let myAdd: (baseValue: number, increment: number) => number =
    function(x, y) { return x + y; }

默认参数

随便写那个位置,但是写必传参数前是,默认参数 需要传入undefined,写最后使用时可 不传参数

function buildName(firstName = "Will", lastName: string) {
    return firstName + " " + lastName;
}
let result4 = buildName(undefined, "Adams");     
// okay and returns "Will Adams"

可选参数

必须写在所有参数后面

function buildName(firstName: string, lastName?: string) {
    if (lastName)
        return firstName + " " + lastName;
    else
        return firstName;
}
let result1 = buildName("Bob");  // Bob
let result4 = buildName("Bob", "Adams"); //Bob Adams

剩余参数...

function buildName(firstName: string, ...restOfName: string[]) {
  return firstName + " " + restOfName.join(" ");
}

let employeeName = buildName("Joseph", "Samuel", "Lucas", "MacKinzie");

函数中使用this

在函数参数里面定义this的指向,否者为any类型或者编译器设置了–noImplicitThis标记报错

interface Card {
    suit: string;
    card: number;
}
interface Deck {
    suits: string[];
    cards: number[];
    createCardPicker(this: Deck): () => Card;
}
let deck: Deck = {
    suits: ["hearts", "spades", "clubs", "diamonds"],
    cards: Array(52),
    // NOTE: The function now explicitly specifies that its callee must be of type Deck
    createCardPicker: function(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();

alert("card: " + pickedCard.card + " of " + pickedCard.suit);

重载

当一个函数 传入的参数有多种可能时,可以使用多个函数类型定义,
编译器会按照书写顺序使用重载定义,如果匹配的话就使用,则 一定要把最精确的定义放在最前面。

注意,function pickCard(x): any并不是重载列表的一部分

//下面存在两个重载:一个是接收对象另一个接收数字。 以其它参数调用 pickCard会产生错误。
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 {
    // Check to see if we're working with an object/array
    // if so, they gave us the deck and we'll pick the card
    if (typeof x == "object") {
        let pickedCard = Math.floor(Math.random() * x.length);
        return pickedCard;
    }
    // Otherwise just let them pick the card
    else if (typeof x == "number") {
        let pickedSuit = Math.floor(x / 13);
        return { suit: suits[pickedSuit], card: x % 13 };
    }
}

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);

let pickedCard2 = pickCard(15);
alert("card: " + pickedCard2.card + " of " + pickedCard2.suit);

被重新赋值时兼容

参数 少的可以兼容参数多的,多的不能兼容少的

let x = (a: number) => 0;
let y = (b: number, s: string) => 0;
y = x; // OK
x = y; // Error

let x = () => ({name: 'Alice'});
let y = () => ({name: 'Alice', location: 'Seattle'});
x = y; // OK
y = x; // Error, because x() lacks a location property

你可能感兴趣的:(TypeScript,TypeScript-)