Toggle Menu

Insights / Tech Tips / 3 Common Questions for TypeScript Beginners

July 10, 2017

3 Common Questions for TypeScript Beginners

5 mins read

TypeScript was released in 2014 and is becoming more commonly used every day in the development world. It is not brand new, but not quite a staple yet. A lot of developers already know it well, and are able to develop all kinds of applications with it.

On the other hand, many developers who do not know or have just heard of Typescript in passing might be intimidated by it. Is it Javascript? Is it a new language? Is it difficult or worth it to learn?

In truth, TypeScript is simple. As a developer with any amount of experience with an object-oriented language, you should not be hesitant to learn TypeScript. In fact, you probably already know much more than you think you do.

So really the question becomes: what exactly is TypeScript? Is it Javascript? Is it a new language? Is it worth it to learn? Knowing the answers to these basic questions will provide a solid foundation to begin integrating TypeScript into your development career.

1. Is it JavaScript?

No. TypeScript is a superset of JavaScript, meaning it extends the language. Any code written in JavaScript is also valid TypeScript code, but not all TypeScript code is JavaScript. It’s the same relationship that a square (TypeScript) and a rectangle (JavaScript) have.

Code that is written in TypeScript is run through the TypeScript compiler and converted back to any version of Javascript (this is configurable). This gives us the first actual advantage of TypeScript: compile-time errors. This eliminates the process of running the code in a browser, discovering it is not working, and pulling up the console just to see a syntax error. The compiler will recognize errors in the code right away, saving you time testing bad code that was never going to work in the first place.

2. Is it a New Language?

No. TypeScript improves upon JavaScript and adds 1) classes and 2) typed variables. It also includes interfaces, inheritance, and much more but we won’t touch on those. To put it simply, TypeScript is just object-oriented JavaScript! There are no new principles or concepts to learn – if you have ever worked in an object-oriented language such as C#, Java, Ruby, etc., you are ready to code in TypeScript right now. Let’s take a look at an example:

A simple greeter function in JavaScript:

greeter.js

And it’s equivalent in TypeScript:

greeter.ts

Although the code has the same functionality, the new TypeScript code is cleaner, shorter, and easier to read. The function in JavaScript has been replaced with a class in TypeScript. There is a constructor in the new class that takes in a typed parameter (this is optional), which in our case is a string. Also note the conventional .js extension in the JavaScript file while the TypeScript file has a .ts extension.

Adding types to JavaScript functions is a seemingly crazy concept, but the creators of TypeScript made it a point to keep the types simple. There are only four – string, boolean, number (no long/double/int/Integer/decimal etc.), and Any (for structures).

For the curious, the TypeScript playground is a great resource to check how Typescript converts back into JavaScript.

3. Is it Worth it to Learn?

TypeScript is still very new and growing in its usage in the development world. It will surely have its place in the community and is a very useful skill to have. At the same time, learning TypeScript can happen on one’s own time if when having a previous knowledge of JavaScript. You can rewrite all of your JavaScript code with TypeScript if you want, or just pick and choose small snippets. It is also possible to just change your .js files to .ts and leave them exactly the same! Picking up TypeScript does not have to be one big jump.

Summary

TypeScript is nothing to be scared of. It is a superset of JavaScript that adds object-oriented programming principles and typed variables. The TypeScript compiler gives the developer a way of seeing errors in real time instead of waiting for them to pop up in the developer console. It is a versatile language that is only growing in use and will be around for a while.

If you are looking for somewhere to learn more, Pluralsight is a great place to start learning the language. There are also plenty of small tutorials to get a good grasp of TypeScript right away. The sooner you jump in to it, the sooner you’ll stop seeing TypeScript as a new confusing language and start seeing it as a simple language with which you can build any application.

Stay tuned for these blogs and more from ng-conf 2017 by the ExcellaJS team:

You Might Also Like

Resources

How Federal Agencies Can Deliver Better Digital Experiences Using UX and Human-Centered Design

Excella UX/UI Xpert, Thelma Van, join host John Gilroy of Federal Tech Podcast to discuss...

Resources

Data Scientist: What Are They and Why Are They Important?

Excella Practice Director, Artificial Intelligence and Analytics, Aaron Pujanandez join host John Gilroy of Federal Tech Podcast to discuss...