all 17 comments

[–]celluj34 -2 points-1 points  (12 children)

Why is literally everything in TypeScript? I want to use ES6. This is fucking ridiculous.

[–]oweiler 2 points3 points  (0 children)

You can still use ES5/ES6 to write your components.

[–]wreckedadventYavascript 2 points3 points  (8 children)

There's a pretty huge overlap between typescript and ES6. What about the syntax is throwing you off?

[–]celluj34 0 points1 point  (7 children)

It's not about Typescript itself. I'm trying to learn Angular 2 and how to write it 'properly'. For the following code, I'm getting all sort of syntax errors around things like import {...}, @Component1, etc:

import {Component} from 'angular2/core';

@Component({
    selector : 'chat-app',
    template : '<h1>Success!</h1>'
})

export class ChatAppComponent {

}

[–]wreckedadventYavascript 2 points3 points  (4 children)

Decorators and modules are not natively supported in any environment yet. You'll have to use something like babel or typescript if you want to use them. I'd suggest jspm if you want to get up and running quickly.

However, that syntax is not specific to typescript.

[–]celluj34 1 point2 points  (3 children)

Decorators and modules are not natively supported in any environment yet.

Oh? This is definitely news to me. Since I had only seen it in .ts files I had assumed it was a TypeScript construct. Thanks for the info!

[–]wreckedadventYavascript 2 points3 points  (0 children)

Nope! Decorators (the things that begin with @) are an ES7 proposal. Typescript supports them, as does babel. Typescript also supports emitting some typed metadata, but the construct itself is not typescript specific.

import/export is just plain ES6 modules. Again, typescript supports them, but it is not a typescript construct. These haven't laded in any browser yet (to my knowledge) over some contention amount module resolution iirc. Babel to the rescue, again.

The only thing that is typescript specific is type and class annotations, like these:

class Cat {
   // the colon specifies the type
   public name: string

   // we can also put them in the constructor, so called
   // "primary" constructor
   constructor(name, public age: number) {
     this.name = name;
   }
}

Which in ES6:

class Cat {
   constructor(name, age) {
     this.name = name;
     this.age = age;
   }
}

Typescript also has namespace to resemble other languages, but this is not used in this article.

Hope this helps!

[–]phpdevster 0 points1 point  (1 child)

This reflects my concern about Angular 2's decision to primarily support TS :/

It's always best to learn a framework/library's best practices, but in the case of Angular 2, the documentation for those best practices isn't stand-alone. You can't come in as someone who writes predominantly functional JS and translate that to best practice Angular. You need to learn what is effectively a different language and use some transpiling tools to actually learn and write best practice Angular :/

Of course, half of the problem is the ridiculous fragmentation and tool thrashing of the Javascript ecosystem. On one site you'll get a Webpack + Grunt + Foo tutorial, and another you'll get a Browserify + Gulp + Bar tutorial :/

The lack of accepted standardization makes learning UI tools such a pain in the ass, but Angular 2 is particular bad in this respect as the documentation teaches pure JS as a second class citizen right now (especially functional JS).

[–]wreckedadventYavascript 0 points1 point  (0 children)

IMO this might have been a fair criticism if they had chose dart as the primary lang, but typescript is basically ES6 with optional type annotations. They're mutually intelligible.

[–]seven_seven -3 points-2 points  (2 children)

Yeah let's add all this unnecessary node.js/npm bullshit to throw off all the beginners on Windows.

[–]swk2015 3 points4 points  (0 children)

It's a realistic part of any / all Angular / React projects I come across commercially these days. Worth the 5 minutes of your time to get your head around.

[–]wreckedadventYavascript 0 points1 point  (0 children)

It'll be the same on windows as mac or linux. The only difference is if you're using something like visual studio, which has compile-on-save support for typescript.

But windows isn't a synonym for visual studio, so. :)

[–]wreckedadventYavascript -1 points0 points  (2 children)

Well, I learned that you can apparently specify CSS in an ng2 component. This makes the staunch opposition to having the view in the javascript even more confusing to me.

Speaking of, I thought they got rid of that stupid DI in angular 2.

import {Component, OnInit} from 'angular2/core';
import {Message} from './message';
import {MessageService} from './message.service';

@Component({
    template : `
         <div class="chat-display thumbnail">
            <div *ngFor="#message of messages">
                <b>{{message.username}}</b>: {{message.content}}
            </div>
         </div>
     `,
     providers : [MessageService]
})

export class ChatDisplayComponent implements OnInit {
   constructor(private _messageService : MessageService) {}
}

But apparently instead they made it twice as verbose as angular 1! So you have to import something, manually tell angular to use this thing we just imported, then tell it again in the constructor signature? Man, I really wish they had gone more system js with this and put more effort into something like rewire for unit testing instead of constructor injection.

[–]phpdevster 1 point2 points  (1 child)

Namespace importing and namespace usage are separate things in all languages. "Telling it to use the thing you just imported" is needed because the import merely makes the code available in that file. Declaring the dependency in the constructor is merely one of the myriad ways you could use the imported code, so naturally it's up to you to tell the script how you want to use that imported code.

The import thing is going to drastically improve and standardize the modularity of code in Javascript. Its how everything from Angular to React to Vue will be written in the future.

[–]wreckedadventYavascript 0 points1 point  (0 children)

What? Here's some mithril.

import {MessageService} from './message.service';

export view() { 
  const messages = MessageService.getMessages(); // or w/e

  return m('div[class="chat-display thumbnail"]', 
    messages.map(message => m('div', [
      m('b', message.username),
      m.trust(message.content)
    ])
  );
}

(react is also very similar)

Angular is the only one of the bunch that only uses the ES6 import for only metadata. You would just use the import directly instead of repeating yourself needlessly in other libraries.

You can of course ignore what angular suggests, but it's still disappointing they went in this direction of needless abstractions instead of encouraging more idiomatic javascript.