9

I work with Angular 1 for years now, and I'm starting to learn how to use Angular 2 instead.

But before writing any line of code, I struggle to whether I have to use TypeScript or JavaScript.

  • What are the pros and cons using TypeScript ?
  • What are those for using plain old JavaScript ?

Bonus question :

  • Is there a best practice for choosing between JavaScript and TypeScript in an Angular 2 context ?

I don't know TypeScript but from the few pieces of code I have seen, it seems the major features are :

  • Type hinting

  • Prototype mechanisms hidden under a most common used Class syntax.

My first impressions after browsing the web :

Type Hinting :

I love type hinting, but I think it's a little overkill to use a new language just to add this feature and will produce more work for the devs to use libraries with loose type (from the docs, there is some ways to do this).

Class-based styntax :

What about the new class syntax (which is close to the ES6) ? The Prototypes are the most misunderstood feature of JavaScript but I think it's more important to understand how it works (to know what you really do) than simply put it aside.

ES5 is a Prototype-based programming language so why do you want to use a pseudo Class-based programming language, which will eventually produce prototypes ? It seems weird to me.

Code generation :

The last point is about code generator. I've already used code generator in other context, and I don't really like it : in most case, a human will produce a better written code (I have taken a look to the generated js : anonymous functions, anonymous function everywhere !) and more concise (when it comes to load the generated js files in the browser the size is important to reduce loading)

To conclude :

For instance I'm not really convinced of the interest to learn TypeScript instead of sticking to ES5 JavaScript. So I would like to have feedbacks from the community to know the advantages of using TypeScript.

Elorfin
  • 2,487
  • 1
  • 27
  • 50
  • 1
    See [Should I use typescript? or I can just use ES6](http://stackoverflow.com/questions/30807612/should-i-use-typescript-or-i-can-just-use-es6). Angular 2 itself is written in Typescript, so it will eventually be the standard used with Angular 2 apps. – dsgriffin Jan 15 '16 at 10:24
  • 1
    You pretty much already listed all the pros and cons. There's no fundamental technical reason to choose one or the other, Angular is just a Javascript library, and everything is compiled down to Javascript anyway, so it'll work one way or the other. How the community and ecosystem will turn out in the future we cannot tell you. Take your pick. – deceze Jan 15 '16 at 10:28

2 Answers2

6

I think the advantage of Typescript is the transpiler to Javascript. When there is a new feature in Javascript the transpiler is able to create better Javascript while my Typescript isn't changing.

What I also like about Typescript is the type checking itself. It already saved me from coding errors simply because the types didn't match, also typed arrays helped me to structure like:

let members: array<People>.

It forces other coders to provide the right object type.

One could also argue native Javascript is the way to go since it removes the transpiling process and is native.

From the Angular quickstart:

Typescript

import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent { }

Javascript

function(app) {
  app.AppComponent =
    ng.core.Component({
      selector: 'my-app',
      template: '<h1>My First Angular 2 App</h1>'
    })
    .Class({
      constructor: function() {}
    });
})(window.app || (window.app = {}));

As the example above illustrates, I prefer Typescript since my code looks cleaner and therefore better to understand.

Bas van Dijk
  • 9,933
  • 10
  • 55
  • 91
4

There is no correct answer to this question. However I was asked to work on Angular2 for the past month and I've been doing some research and development on it. So maybe some points can help you out:

  • Code hinting is still not mature in the IDEs because ts is still new and its still changing.

  • I agree that they should concentrate on prototype which is the strength and orientation of JS, if they want OOP they should use an OOP language instead, but what can I say, no disscusion about it is gonna change anything.

  • I've been working on it using TS and I can tell you, compared to js, the thing is more readable, takes whole lot more to setup, but tbh there's only one thing that should matter to you and its why I went the TS way:

There's pretty much no documentation on Angular 2, not even the official API is fully written yet, the little bit of pieces you'll find here and there are either outdated with alpha syntax that doesn't work anymore or its written in TS. I've even had to go through the src code to understand how am I suppose to call something because of the lack of documentation on it. So if you are starting now, I strongly suggest you to go that route. Just so you know what you are getting into, check the Angular.io page and see the Developer guide, there's 4 sections written in JS and 15 in TS, its obvious they'll push for TS.

"Is there a best practice for choosing between JavaScript and TypeScript in an Angular 2 context ?"

No, its a matter of preferences, how big your project is, how much estructure you want in your code, how easy you want to spot defects at the cost of more time developing, etc...

Langley
  • 5,326
  • 2
  • 26
  • 42