TypeScript: Supercharging enums with modules.

When using a language like Java you can use enums to do some very interesting things. For example a Java enum can look like so: public enum Directions{ EAST ("E"), WEST ("W"), NORTH ("N"), SOUTH ("S") ; /* Important Note: Must have semicolon at * the end when there is a enum field or method */ private final String shortCode; Directions(String code) { this.shortCode = code; } public String getDirectionCode() { return this....

TypeScript: How to check for types. Using tagged unions.

Working with TypeScript has been amazing. One of the most common things I’ve been finding my self checking was whether one variable is of a certain type. Let’s look at an example. interface Mammal { name: string; } interface Horse extends Mammal { canRun: boolean; } type Animal = Mammal | Horse; const animalThings = (animal: Animal) => { if (animal instanceof Horse) { // Error: 'Horse' only refers to a type, but is being used as a value here....