A Tinder Date with TypeScript
EDIT 11/30/2020: Post-publishing this story, I’ve spoken with some very intelligent people with more TS experience than myself. I can now see tangible benefits to using TS on large, complex projects, especially with multiple contributors. Even as a small example, it would throw an error trying to re-assign a variable to a different type. This is useful. Additionally, it often makes functions, variables, returns.. easier to understand since I know the expected return type. No more second-guessing if something returns a string or boolean.
This post hurts me. No, really. It cuts me deep. I see such value in static typing and declaring what your variable types will be. It makes so much sense that I can never multiply 5, a number, and “banana”, a string, yet Javascript still tries and tells me the result wasn’t a number. Well… yeah.
Enter Typescript. Developed by Microsoft, this is a superset of Javascript that allows you to declare types for every variable in use across your program. The idea being that it can detect any bugs that may occur before they happen. Neat!
If only it actually worked that way. First, I thought that I’d be able to code like usual and declare types on a whim. We’re close to that, but if TypeScript isn’t able to infer what something should be, it’s going to yell at you to declare a specific type.
Wait…. my event needs a type too? This is getting weird. Yep, you have to declare the type of your event, for instance a MouseClick. That’s annoying.
Oh well, this is going to protect me from bugs in my code, right? Check out this simple counter I made. The counter has an initial state of 0, so TypeScript infers that it’s a number. Cool. So if I add a number to it, we should be solid.
Oh, what the hell. Now. To be fair, this is just how Javascript was written. My personal expectations here would require modifications to the underlying language, which I’m sure makes it vastly more complicated. TypeScript will protect you in the instance of a variable being declared as one datatype, then being reassigned to a different datatype.
This is a feature I find valuable that can be found in languages like Rust. Let’s look at how Rust handles this. If I add two numbers, everything is great. Add a number and a string? NOPE. Because Rust understands you can’t add those items together. Nice try. Let’s have _2 = 2 and _two = “2”.
I wanted to love you TypeScript, but you’re abusive and aggressive before I compile and you can’t even get the job done.