Tips and Tricks with TypeScript
One of my most recent projects has been tackling how to model the card game, Love Letter. For those who've seen me present my How Functional Programming Made Me a Better Developer talk, you might recall that this was my second project to tackle in F# and that even though I was able to get some of it to work, there were some inconsistency in the rules that I wasn't able to reason about.
While implementing in TypeScript, I came across some cool tricks and thought I'd share some of them here, enjoy!
Swapping two variables
A common enough task in programming, we need to swap two values around. When I was learning C++, I had a memory trick to remember the ordering (think like a zigzag pattern)
You can do the same thing in TypeScript, however, you can remove the need for the temp
variable by using array destructuring. The idea is that we create an array which contains the two variables to swap. We then assign this array to destructured variables (see below)
Drawing a Card
One of the common interactions that happens in the game is that we need to model drawing a card from a deck. As such, we will have a function that looks like the following
This absolutely works, however, we can use the rest operator (...) with array destructuring to simplify this code.
This code should be read as assign the first element of the array to a const called card
and the rest of the array to a const called restOfDeck
.
Drawing Multiple Cards with Object Destructuring
This works, however, we don't actually care about result
, but the specific values card
and restOfDeck
. Instead of referencing them via property drilling, we can use object destructuring to get their values.