Typescript: A Java Developer Perspective

Recently I followed an online Angular course, giving me an in-depth experience of both TypeScript and Angular 4. This was my first experience with both technologies and they look very promising, but I can’t help to have a couple of remarks about some choices that have been made. In this blog I start with giving my opinion about some of the choices made in TypeScript. In the following blog post I will continue with my remarks about Angular 4.

Because I have only seen TypeScript in combination with Angular 4, I may confuse features by mistaking them to be part of the one or the other. I am by no means an expert in either Angular 4 or TypeScript, so please forgive me when I make mistakes like this. I would also like to hear your experiences and comments.

I have worked with JavaScript in the past, and I always had a hard time debugging it, and keeping track of fields and methods of a certain field.
I have often called methods or accessed fields that did not exist. With TypeScript this is a lot better because of the static type analysis and the compilation step. At compile-time you are now made aware of the fact that the method or property does not exist, which makes debugging a lot easier. The usage of a proper IDE also contributes to this, but is not related to TypeScript at all.

It is because of this, and the fact that there is Type in the name, that I am a confused to why the static typing is made optional. The most logical explanation for this would be that TypeScript is superset, or extension of JavaScript. But what exactly do you gain by having the ability to write plain JavaScript in TypeScript? Another weird typing feature is that a method can return multiple different types. It is for instance possible to have the following method:

public foo() : number | string

How can this ever the statically checked for the type? Assigning the return value to a field means you still have to check the type at run-time, which may cause a lot of errors that go unnoticed.

Placing the type after the field also feels a bit weird sometimes. For methods this is not a problem because the method body is written on the next line anyway (except from the curly brace). But a field that is initialised during definition looks very weird to me. The separation between the actual field name and the value makes it harder to read, and I often misread it as the property number with value 5.

private bar: number = 5;

The verbosity at which you need to access fields and methods by always explicitly using the ‘this’ keyword is pretty annoying as well. This is of course required as not all methods need to be located inside of a class. This means that omitting the ‘this’ keyword means you are calling a ‘global’ function compared to calling a class method when specifying the ‘this’ keyword. In Java this is not required, as all method need to be located in a class no matter what, but even in C++ this explicit usage of ‘this’ is not required. This makes me think that there are other issues that cause the ‘this’ keyword to be required to successfully identify the method to call, perhaps the lack of namespaces.

Finally there is the big inconsistent usage of keywords to mark fields and variables as constant. While local variables use the ‘const’ keyword, this does not work for fields of a class. Instead you need to mark them as ‘readonly’. The different types of variables that can be created ‘let’ and ‘var’ is again a way to bypass the typing system which I think is a bad idea. Constructors are treated specially as well, as it is possible to define a field in the constructor parameter list. This causes an inconsistency with other fields that are not passed on in the constructor. This obfuscates the list of fields and in general is a practice I do not do. I prefer to explicitly assign all the fields in the constructor over the shorthand notation.

export class OverviewComponent implements OnInit {
public errorMsg: string = “”;
constructor(public service: Service) {}
}

Nevertheless I think TypeScript is a big improvement over JavaScript and I am very grateful that I was able to use it as it made me get my work done much faster and took away lots of the annoyances I experienced with JavaScript.

Advertisements

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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.