destroytoday.com

TypeScript

TypeScript is one of those technologies that I missed the boat with early on, so I didn’t feel compelled to follow along with it. Since then, a large majority of the dev community went full-in, which makes it hard to ignore now, so I’d be foolish not to see what the fuss is about—at the very least, I’ll learn more about TypeScript, so I have more context and experience when people talk about it. Also, this site is still early enough in development that TypeScript is much easier to implement now, and either embrace or reject, than attempt a complex rewrite later.

For those who are unfamiliar with TypeScript, it is described as “JavaScript that scales ... a typed superset of JavaScript that compiles to plain JavaScript.” TypeScript brings static type-checking to JavaScript that will catch any type-related issues during compile time—saving you from runtime errors. This means instead of writing a function:

function fooToBar(str) {
  return str.replace(/foo/g, 'bar')
}

fooToBar(1337) // Runtime error: str.replace is not a function

...and running the risk of someone passing a non-string argument, I could write:

function fooToBar(str:string) {
  return str.replace(/foo/g, 'bar')
}

fooToBar(1337) // Compiler error: Argument of type '1337' is not assignable to parameter of type 'string'

...which would throw an error at compile time if you tried to pass something else. While this also makes it easier to understand unfamiliar code by documenting props and responses, TypeScript also enables code completion and IntelliSense for compatible IDEs, like VS Code. This lets you hover a method or argument to see more info about it, but also indicates the compiler error in the IDE as well.

I am cautiously approaching TypeScript with a strong bias. After CoffeeScript, I’m pretty scarred when it comes to writing invalid JavaScript and adding another layer to the build process. The beauty of a personal website, however, is that it’s the perfect opportunity to experiment with technologies like this. A lot of this site’s stack is overkill, but this site gives me the opportunity to learn new things in a safe environment. If an experiment doesn’t work out, I can easily remove it and move on—compared to an early stack decision with an app that might come back to haunt you forever. *shakes fist at Angular & CoffeeScript*