Enums in TypeScript

Testing typescript

enum Suit {
Heart = "Heart",
Diamond = "Diamond",
Club = "Club",
Spade = "Spade"
}

enum Rank {
One = "One",
Two = "Two",
Three = "Three",
Four = "Four",
Five = "Five",
Six = "Six",
Seven = "Seven",
Eight = "Eight",
Nine = "Nine",
Ten = "Ten",
Jack = "Jack",
Queen = "Queen",
King = "King",
Ace = "Ace"
}

function getIcon(suit: Suit): string {
switch (suit) {
case Suit.Heart:
return "♥️️";
case Suit.Spade:
return "♠️";
case Suit.Diamond:
return "♦️";
case Suit.Club:
return "♣️";
}
}

class Card {
public readonly suite: Suit;
public readonly rank: Rank;

constructor(suit: Suit, rank: Rank) {
this.suite = suit;
this.rank = rank;
}

visual() {
return `${getIcon(this.suite)} ${this.rank}`;
}
}


class Deck {
private cards: Card[];

constructor() {
this.cards = [];
}

addCard(card: Card) {
this.cards.push(card);
}

size() {
return this.cards.length;
}

reveal() {
this.cards.forEach(card => {
console.log(card.visual())
});
}
}

// This makes a standard 52 card deck
function makeDeck() {
let deck = new Deck();
let suits: Array<keyof typeof Suit> = Object.keys(Suit)
as Array<keyof typeof Suit>;
let ranks: Array<keyof typeof Rank> = Object.keys(Rank) as Array<keyof typeof Rank>;

suits.forEach(suit => {
ranks.forEach(rank => {
deck.addCard(new Card(Suit[suit], Rank[rank]))
})
})

return deck;
}