loading...
Cover image for I Chose TypeScript For the Nostalgia

I Chose TypeScript For the Nostalgia

aunyks profile image Gerald Nash ⚡️ ・4 min read

Back when I was 13 years old and beginning my freshman year of high school, I wasn’t sure what programming language I wanted to learn first. After browsing a lot of fora and viewing a few old YouTube videos, people almost always recommended Java as a practical and profitable first language. So, that’s what learned.

Fast forward to Junior year and I’ve had my fair share of class files, NullPointerExceptions, and a gamut of compilation errors. I’m breezing through my AP Computer Science course (which, as you guessed, teaches Java), and I’m starting to look into open sourcing my work and building more practical projects.

I decided to write a web server with Java EE and Apache Tomcat, and, since I grew tired of what felt like writing more syntax than semantics, let’s just say I got much too frustrated with Java at this point, so I ventured into a different ecosystem.

After some Google searches, I discovered Node.js and Express. After seeing an example Hello World server, I fell in love with the conciseness and abstraction that these technologies provided.

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

And so, I kind of became a JavaScript developer. I starting learning about callbacks, prototypes, and other cool stuff JS provides new developers. I started using Javascript for projects of all kinds: front end, back end, desktop, command line, etc. I fell in love with JavaScript’s ubiquity and simplicity (dynamic typing and not having to use semicolons seemed so awesome…at first).

But, now I graduated high school and I’ve written thousands of lines of JavaScript. The things that originally brought me over to the language are now reasons for me to dislike it. I’m starting to see the flaws in dynamic typing, non-strict syntax, and other facets of the language. I’m starting to look for a compile-to-JS language to keep my faith before I (maybe) leave the ecosystem. Java’s syntax and typing never looked so attractive…

And, that’s when I found TypeScript, a typed superset of JavaScript. Coming from ES5, I liked that TypeScript automatically included ES6 and beyond features (like that sweet, sweet class syntax) without needing complicated Babel configurations. At this point, I was happy to see some syntax that somewhat reminded me of my Java days.

But, ES6 doesn’t natively provide great typing functionality: that’s where TypeScript’s true advantages come out. In the same way that Java’s types are enforcements, TypeScript’s types are suggestions. So, you have the opportunity to see how types semantically flow throughout your application similar to compiled languages like Java and C#, but you don’t necessarily have to abide by type inconsistencies, because it’s still JavaScript under the hood. And that’s good enough for me.

The syntax is essentially JavaScript as you know it, except for type declaration files. Take a look at the example code below. Borrowed from this page.

var burger: string = 'hamburger',     // String 
    calories: number = 300,           // Numeric
    tasty: boolean = true;            // Boolean

// Alternatively, you can omit the type declaration:
// var burger = 'hamburger';

// The function expects a string and an integer.
// It doesn't return anything so the type of the function itself is void.

function speak(food: string, energy: number): void {
  console.log("Our " + food + " has " + energy + " calories.");
}

speak(burger, calories);

Like I said before, it looks just like normal JS, except you optionally define the types of each variable and function return with colon syntax. No trouble!😉

Although TypeScript’s typing is more of a recommendation, it’s close enough to remind me of a more strictly typed language like my starting point, Java. It helps provide a bit of needed structure when building large scale projects and applications by adding typing, which is what keeps languages like C#, Java, and C++ in use.

Although I grew tired of the forced verbosity that compiled languages require of their developers, it turns out that I may have taken it for granted. When given the freedom of dynamic typing, optional semicolons, and the rest of the works, it seems that it gets noticeably harder to build and debug large applications because of the lenience. TypeScript’s illusion of static typing provides that bit of structure that one might need to keep code clean and readable. As you can see, I chose TypeScript for the nostalgia. What might you choose it for?

Thank you

Thanks for reading this! Feel free to give me some feedback on how it was and whether or not you would ever use TypeScript, and why.

Twitter, Github, Snapchat, Medium, Instagram

Posted on by:

Discussion

markdown guide
 

Very honest and well-articulated account of the kind of process we all go through when navigating technology choices. A lot of devs will come up with really complicated explanations about using words like idempotency as if development is a stateless activity that has nothing to do with our prior experiences and the tastes we've built up over time.

I've heard people build up impassioned "logical" arguments for their defense of a certain tech or pattern, when they're really just expressing a reaction to their singular experience with the technology that fails to account for the million things that could have affected their experience.

I feel like this post is a more honest introspection of choices than I typically see. Great stuff, Gerald.

 

The best reason to use TypeScript is that IDEs can actually make writing TypeScript code a decent experience, and they really can't help much with JavaScript because of the ambiguity of everything.

Just try and find an IDE that can reliably do refactoring, auto-completion, inspections, etc. on JavaScript. For TypeScript that's almost trivial. TypeScript is really the way to make JavaScript into a manageable language to write quality software in, and I will likely never touch pure JavaScript again.

I've been using JavaScript in one form or another pretty heavily for a long time, and in many ways the ecosystem is just getting worse over time as people keep adding more and more tools to make working with the garbage language bareable. It's becoming more and more difficult to set up a working tool chain to work with, and because of the complex nature and constant re-inventing the wheel with those tools everyone else's example code is never compatible with your system as it's either built for some other tool chain or it's just built for older versions of the tools.

Just as a quick example how to manage the various steps in your no doubt fairly complicated build process: npm scripts, gulp, grunt, webpack, as well as tools built into IDEs and even Makefiles are all common options

Additionally since the language itself is really not very good for almost anything, and is missing so many things that would be considered "bare basics" in any decent language, you end up with a huge amount of dependencies of varying degrees of quality and with varying degrees of care put into their design and releases. This causes your code to unexpectedly break with updates (unless you use yarn) or just have unexpected behavior or performance characteristics in general.

Since it's not really reasonable to expect all browsers to start supporting another language, what I'm really hoping for myself is that one of those languages that compile into JavaScript (e.g. Kotlin) or maybe some Emscripten -powered thing sets up a great ecosystem and takes over so we can get past this giant mess that JavaScript is right now. While that is not the case, TypeScript is definitely a step to the right direction and reduces the need for some of those tools, and makes you much more productive.

By the way, on the point of although TypeScript’s typing is more of a recommendation, you should configure your tsconfig.json to not make it a "recommendation" but to make it a requirement.

 

I recently started adopting TypeScript for my JavaScript projects at work, and I've found that it's been a truly useful tool for all the reasons you've mentioned above.

Great read!