Site icon Treehouse Blog

Why TypeScript is Hot Now, and Looking Forward

It’s never been a better time to be a JavaScript developer! JavaScript is everywhere now – it’s on the front and back-end of a website, and many desktop text editors are powered by JavaScript. In fact you can build a whole range of desktop applications using web technologies as their foundation. As the complexities of your JavaScript applications increases it’s important to keep your code under control before it spirals in to a mess.

Enter TypeScript

TypeScript is ECMAScript 6 (ES6) with optional typing.

ECMAScript 6

ECMAScript is the standardized specification for the JavaScript language. It’s sometimes referred as ECMAScript Harmony or ES.next. At the time of this writing, JavaScript is currently at ECMAScript 5. A few browsers implement some of the ES6 specification, but adoption is increasing daily. Since ES6 is backwardly compatible with the ES5 syntax, you can start writing TypeScript without changing any of your existing code. However, for the most part, learning the TypeScript syntax is learning the ECMAScript 6 syntax, so you’re heading toward the future of JavaScript and not learning a completely new syntax, like you would in CoffeeScript.

Optional Typing

Where TypeScript comes in to its own is in its optional typing. A typed language like Java, C# or Objective-C requires you to specify the type of the variable when declaring it.

Declaring a string in Java would look like: String name = "Andrew";, in C# string name = "Andrew"; and in Objective-C it’s NSString name = "Andrew".

In TypeScript it would be: var name: string = "Andrew". But with it being optional, it can be just plain var name = "Andrew". In strictly typed languages you have to declare everything all the time, with TypeScript you don’t!

Having types in your script allows text editors and IDEs to give you intelligent hints quickly without having to run your code.

It also helps with autocompletion of your code too. Other text editors autocomplete based on the text written in your project files, it doesn’t know anything about the type of each variable so you may end up passing a similarly named variable in to a method call but it’s the wrong type of object. However with TypeScript, code editors can have a more intelligent approach and suggest more appropriate variables to pass in to a function call. It’s a real productivity boost. It’s almost as if the code is writing itself.

It also reduces the need to look up documentation so frequently since the code is annotated with the types needed for a method call or what will be returned from a method call.

TypeScript Everywhere?

Because of these productivity wins from cleaner ES6 code, autocompletion, and hinting from optional typing, TypeScript is being adopted in to major projects, like AngularJS 2.0 and Ionic Framework 2.0.

You can also leverage TypeScript in JavaScript projects not written in TypeScript in the first place. TypeScript has definition files that allow you to get these productivity boosts when coding almost in any environment from the Browser to Node.js. You get all the autocompletion and hinting as if they were written in TypeScript.

Typing (May) Be Coming to a Language Near You

Some programmers prefer dynamic typing, that is, you don’t have to declare the type when programming your code. Perhaps the reason is the code is less verbose. However, there seems to be a trend at the moment where many programming languages are converging on ideas, from generics, immutable variables, asynchronous programming, optional typing, and even intelligent compilers. Even the creator of Ruby, Yukihiro Matsumoto, said static typing could be coming to version 3.0.

Having typing in your code not only helps programmer productivity but also helps interpreters and compilers to optimize the running of your code.

Next Steps

Check out the TypeScript website and watch the introductory screencast on the homepage. You can play around with TypeScript in an Interactive Playground. You should also download the cross-platform text editor with intelligent code completion, Visual Studio Code. You can also upvote TypeScript on our JavaScript content roadmap.

Conclusion

There are massive productivity boosts when using TypeScript because of its cleaner ECMAScript 6 syntax and optional typing. It looks toward the future of JavaScript, not inventing a completely different syntax. TypeScript’s popularity seems to only increase since its being adopted by popular frameworks like AngularJS. Optional typing seems like its a feature coming to your favorite dynamically typed language soon, too. Who knows, will TypeScript become a newer version of JavaScript? Not only is it a good time to learn JavaScript, but it’s also a good time to get acquainted with TypeScript too!

Exit mobile version