TypeScript versus Idiomatic JavaScript

I’ve been teaching myself TypeScript. Shout out to Thomas Claudius Huber for his book, Getting Started with TypeScript. That slim volume has proved quite helpful. I prefer to learn by doing, so I have been writing a tiny utility library for helping manipulate some special classes of natural numbers and a data structure associated to them.

I think today is the the first time I used the class syntax. I’ve been writing JavaScript for a long time. They did recently add a class syntax to the ECMAScript—which is the standard that defines JavaScript, more or less. But I never felt any need to use it. Exactly which expressions in a language are “idiomatic” is debatable. I don’t yet consider classes to be idiomatic JavaScript.

Here’s where I digress for a moment to explain what I mean by idiomatic because if you are quite new to programming, you’ll hear the word a lot but it may not make sense at first to talk about “idioms” in an entirely fabricated language (versus languages that evolved organically over time, like English).

Simply put, even in an artificial language such as JavaScript, now that so many people have been writing it and communicating in it for decades, idioms genuinely have evolved organically from use—over time. These are not typically examples of new syntax written into the language. More often when people speak of the idioms of a programming language they mean structures and expressions that the fluent writers of that language have come to like and use frequently.

Also, just like in English, there is some degree of difference from one person to another in just what idioms they are familiar with. For example, I recently read somewhere that the expression !!x is idiomatic in JavaScript. That expression would coerce x into a boolean value. (It’s not a single operator. The first ! coerces it and flips the value. The second one flips it back.) Personally, I had never used that idiom or even seen someone else use it, but evidently that writer had seen it and used it and considered it an idiom.

Because JavaScript has never offered a clean, native syntax for encapsulating private data (until recently), people have long used JavaScript’s robust closure mechanism to create private variables. For example, here’s a function that creates an object that maintains private data. Here, the variable myPrivateData is in the closure of the functions obj.getNumber and obj.setNumber, so they both have access to it indefinitely.

function getMyObject () {
  var myPrivateData = 5;
  var obj = {};
  obj.getNumber = function () {
    return myPrivateData;
  obj.setNumber = function (val) {
    // some kind of security or error catching
    myPrivateData = val
  return obj;

This kind of construction is an alternative to writing constructor functions. A constructor works with the new keyword as in var x = new Object(). The above example works like var o = getMyObject();. The result is an object o where myPrivateData cannot be accessed directly, but you can get it and set it according to whatever rules the author wants to specify. This sort of thing is very idiomatic JavaScript.

Of course, for years (decades?) people have been trying to make JavaScript into Java. I know Java because that was the main language they used to teach intro computer science classes and object oriented programming back when I was in college. Maybe they still do; I don't really know. And since then I got certified in it. Anyway: my point is that, yes, I know Java. My relationship with Java is passionless. I'm glad I know it, but working in that language is like trying to program while hogtied. If I want a program that writes "hello world" on the screen, I have to create some kind of HelloWorldPrinter object the only purpose of which is to execute one line of code. To me that seems unnecessary. So I never wanted JavaScript to look like Java. I like JavaScript. I like idiomatic JavaScript.

In comes TypeScript, a long-awaited way (for some people) to make JavaScript look a LOT more like Java. TypeScript is a superset of JavaScript. Legal JavaScript is also legal TypeScript, but when writing TypeScript you are supposed to include a lot of extra information about variable types. And it includes syntactic sugar for things like classes. For example, you can write classes using a syntax like the following.

class MySpecialObject {
   private someNumber : number;
   public getSomeNumber () {
      return this.someNumber

Do I like writing this way? I guess it might grow on me. It's not like I get to decide the shape of a language. This is where things are going—not everywhere, but many places. A lot of people are writing in TypeScript now. I don't hate it. I like it actually. But when I look at that piece of Java/TypeScript* I just wrote above, it makes me miss JavaScript idioms. Just a little.

* To clarify: the block of code above is not legal Java. I was being non-literal. In Java, the syntax for declaring types looks slightly different.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s