DEV Community

Leonardo Lana
Leonardo Lana

Posted on

TypeScript: JavaScript for Developers

Unfaithul guy meme using TS and JS

TypeScript is JavaScript made for developers, I say this quote not in a light way, I mean it with every letter of this text. But, I'm getting ahead of myself, let's do some groundwork first.

TypeScript is a "typed superset of JavaScript that compiles to plain JavaScript", accordingly to the official website (typescriptlang.org), but what that phrase means? I will break it down piece by piece

Types scale, concentrated knowledge don't

Being typed means, well, TypeScript's variables have types and they are checked in compilation time. Types can seem to slow down development at first, and devs will say things like: "I know that this object has a name, don't bother me!". But as your code base grows as well as the number of developers, you might forget that one has object has a name field and another one doesn't, so now, you have two options: dig the code to discover every possible field of one object; or ask someone, and neither of these two options is viable in the long run. In this case, if the objects were typed you could easily check what it has or doesn't have. The conclusion about types is:

Types make your code more readable, therefore more maintainable

Types have an extra step of positive additions, and that's broader and better auto-completion since the code tells the editor directly what a type has or doesn't have, the editor can tell the dev with a function call is wrong, or you forgot to initialize some property in the constructor.

TypeScript is more than JavaScript, a superset

You know how you installed Babel in every JS project for the last couple of years, in order to have some handy features like destructors, the same thing applies to TypeScript. Our friend TS has everything plain JS and Babel JS has and more, like functions that are only proposals. The image below explains a little better:

Venn diagram showing how JS overlaps in TS

And everything ends with JavaScript

In the end, TypeScript compiles to our plain old JavaScript, which means the code you wrote is transformed to our plain old JavaScript, ECMAScript 3 to be precise, this may sound bad, but it's not. This transformation allows any browser or cloud provider that can run ES3, be able to run your code, and even IE is in this list.

Bonuses

The tooling around TypeScript is amazing. The compiler accepts a lot of configurations out of the box, even path mapping, say goodbye to ../../../user.ts and hello to src/entity/user.ts and even rules to avoid common errors like using the any type excessively. And TSLint is a comprehensive tool to help developers standardize their code, helping your code base to feel like it was written by a single person.

Conclusion

TypeScript ultimately doesn't help the final user of the system, there will not be a performance upgrade from moving from JavaScript. But developers will feel an upgrade in their quality of life while coding, with type checking, a good linter, and a similar syntax that they are already familiar but in a better package and overall sanity. Please tell me about your experiences with TypeScript and if it helped you?

Oldest comments (0)