6 min read

Why you should use TypeScript for your next project

Get full  Visibility  and  Control over project Budgets, Costs and  Profitability

As a software developer with extensive C# experience, I have always been somewhat skeptical of JavaScript and its ideology. The lack of type checking and the total freedom always seemed just a bit evil; duck typing, monkey patching and automagical type conversion (which is far from flawless) can all cause chaos in code and allow easy typos.


Price&Cost is currently looking for a Senior full-stack developer in Finland and Estonia to help deliver new exciting features to our growing customer base!

Learn more and apply!


Already sold on the idea of using TypeScript? See our tutorial on Using TypeScript with Angular 1.

I am not the only one who thinks JavaScript is not the best language in the world. A myriad of languages have tried to fix JavaScript by replacing it completely. One of these languages, TypeScript by Microsoft, is an appealing option for anyone with a background in strongly typed compiled languages like Java or C#. Here, I’ll discuss some of the biggest reasons for why one should think about using TypeScript instead of JavaScript.

Disclaimer

Before I start, I would just like to say that I know some people are quite fond of JavaScript’s state at the moment and think nothing needs to be fixed. I agree that JavaScript is a capable programming language and that it will become even better when ECMAScript 6 is supported in all major browsers. At the same time, I think JavaScript leaves much to be desired in terms of developer friendliness and usability.

Origins, credibility and reliability of TypeScript

You always have to be cautious when choosing programming tools because they can change quite quickly—projects get abandoned, technologies depreciate and languages become obsolete. With TypeScript, there you shouldn’t have these problems because it is backed by Microsoft (the creator) and Google (Google’s AngularJS 2.0 uses TypeScript). In addition, Microsoft implemented support for TypeScript in Visual Studio and the recently announced Visual Studio Code. Other supporters include JetBrains’ WebStorm web IDE.

In addition, even if every single TypeScript supporter leaves the game, you’ll still be covered because TypeScript is open source (Apache 2.0). If you’re wondering whether the people who created TypeScript are competent enough to create a good programming language, don’t worryAnders Hejlsberg, the lead architect of C#, was heavily involved in its development.

Stop constantly thinking about types

If you are coding something bigger than a “Hello, World!” application, at some point, you will inevitably forget the type of the third parameter in function X. In fact, it will be hard to remember if the function even has a third parameter. While modern IDEs try to solve this problem by trying to analyze your JavaScript, the results are…well…far from satisfactory. Every now and then, you will stumble upon parts of code that your IDE completely misunderstands, and the tooltip will either be empty or completely wrong. TypeScript solves this problem by being IDE-friendly right out of the box. This means that at any time, IDE can identify the source of the function you are trying to use and show you a tooltip with documentation, ordered parameters and their types.

Changing parameter/variable types

Thinking about which parameter type to use is one thing—you can just find the place where you defined the function and see how the parameters are used inside the function. But what about refactoring? Adding parameters, renaming classes and changing variable types is very shaky ground in JavaScript, and even if you have 100% unit test coverage, you can’t be sure that you have changed every single place in the code that depends on a certain function or class. In TypeScript, this problem doesn’t exist—the compiler will simply not allow you to compile code where, for example, function signatures do not match.

Interfaces

I never thought about the importance of interfaces until I started using JavaScript, which does not have any. In JavaScript, you rely on duck typing, which for me just involves hoping that object A will have features X, Y and Z (and that’s not maintainable). Conventions are great, but when coding a sizable application, it is hardly a good idea to rely on your memory for everything. Matters get even worse when you are not the only developer on the team. It is only logical to start enforcing code contracts somehow, and TypeScript interfaces offer a handy way to do that.

I never thought about the importance of interfaces until I started using JavaScript. Click To Tweet

Strong typing makes you think about your code structure

This may sound weird, but strong typing makes you think about your code structure in detail and forces you to organize your code better than you do when you use vanilla JavaScript, which mostly does not care what you write. TypeScript makes you stop trying to work around the problem with various hacks and tricks and encourages you to fix the source of the problem instead.

Existing JavaScript code

What’s great about TypeScript is that you can start using it in any existing JavaScript project right away; it still compiles into JavaScript and does not require any additional actions. In addition, TypeScript includes the concept of describing external JavaScript libraries using typing files (*.d.ts), allowing to make any external library that you use (e.g. jQuery) a strongly typed one. Just add your JS file to HTML as you normally would (e.g jquery.js), point the TypeScript compiler to where the jquery.d.ts is located and it will type check your use of jQuery functions as it compiles your project!

Conclusion

As you have probably realised by now, TypeScript will not magically solve all of JavaScript’s problems at once. However, it will make your life as a developer easier and much more enjoyable by simply providing you with the right information at appropriate times. This may seem to be a minor improvement when you are developing smaller scale pet projects or writing simplistic animation code. However, you cannot overestimate TypeScript’s usefulness in mid- to large-sized applications as it allows you to focus on the actual business logic instead of trying to remember all the details of a class you wrote three months ago. Give TypeScript a try, and you will not regret it.

 

Easily Forecast and Track project Budgets, Costs and Profitability

Also worth reading

  • Development

    TypeScript tutorial with examples of Angular controller, directive and service

  • Development

    Using TypeScript with Angular 1

  • News and updates

    New: Automatic and Self-Learning Placeholders

Comments

See your potential ROI on an eye-opening demo