If you are developing now in Angular 1.x with an eye toward Angular 2, the best thing you can do is to write your Angular code with TypeScript. Angular 1.x code you write in TypeScript today will look very similar to code you will write in Angular 2 tomorrow (not literally tomorrow!).
At a recent Angular conference, the members of the Angular team from Google stated that one of the best ways to code now in Angular 1.x in preparation for Angular 2 is to use TypeScript.
What is TypeScript?
TypeScript is an open-source programming language that is:
- A superset of JavaScript
- Including all of the current JavaScript syntax, plus much more.
- Any valid JavaScript code is valid TypeScript code. And as such, TypeScript works with all existing JavaScript frameworks, including Angular.
- TypeScript transpiles to plain JavaScript
- So we can deploy the resulting JavaScript files for our Angular application just as we do now.
- And the resulting files execute in any of today’s browsers on any OS.
- Plus there is no runtime overhead of using TypeScript because all of the types evaporate and the TypeScript-only syntax is replaced … leaving only pure JavaScript.
- TypeScript allows us to define strong types
- Editor tooling, such as the new Visual Studio Code editor, use these types for static type checking of our code during development
- This makes development, debugging, and refactoring easier.
- Plus it clarifies the developer’s intent. For example, we can define that a function is meant to take in a string. Not a number, not an object, just a string.
- And TypeScript provides class-based object-oriented programming techniques.
- So we can optionally think of our code in terms of objects with properties and methods.
- And the TypeScript compiler transpiles the classes into JavaScript functions.
Here is the code for an Angular controller, written using the class-based object-oriented programming techniques available in TypeScript.
To learn more about using Angular with TypeScript, check out the Pluralsight course: “Angular with TypeScript“
Enjoy!