Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the pros and cons using ES5 JavaScript for Angular 2 applications and those for using TypeScript? [closed]

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.

like image 869
Elorfin Avatar asked Jan 15 '16 10:01

Elorfin


People also ask

Why we use TypeScript instead of JavaScript in Angular?

Advantages of using TypeScript over JavaScriptTypeScript always points out the compilation errors at the time of development (pre-compilation). Because of this getting runtime errors is less likely, whereas JavaScript is an interpreted language. TypeScript supports static/strong typing.

Can we use JavaScript instead of TypeScript in Angular?

Yes, you can.


1 Answers

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.

like image 109
Bas van Dijk Avatar answered Oct 12 '22 23:10

Bas van Dijk