Created
September 10, 2014 10:49
-
-
Save donnut/7a99207254d23de08ecb to your computer and use it in GitHub Desktop.
Typescript function overloading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var 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") { | |
var pickedCard = Math.floor(Math.random() * x.length); | |
//return pickedCard; | |
return 'notANumber'; | |
} | |
else if (typeof x == "number") { | |
var pickedSuit = Math.floor(x / 13); | |
return { suit: suits[pickedSuit], card: x % 13 }; | |
} | |
} | |
var myDeck = [{ suit: "diamonds", card: 2 }, { suit: "spades", card: 10 }, { suit: "hearts", card: 4 }]; | |
var a: number = pickCard(myDeck); | |
alert(a); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment