【发布时间】:2017-06-27 19:58:24
【问题描述】:
这里是一个函数重载的例子,发布在官方website:
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 };
}
}
当我将此代码放入我的工作打字稿文件时,我收到错误:
错误:(5, 19) TS7006:Parameter 'x' 隐含一个 'any' 类型。
如何使这个例子工作?
【问题讨论】:
标签: typescript