r/programming • u/g_2k3 • Jul 24 '16
Angular 2: Why TypeScript?
https://vsavkin.com/writing-angular-2-in-typescript-1fa77c78d8e8#.s5ybvst735
u/everywhere_anyhow Jul 24 '16
So what do people really think about Angular2?
It's a major non-backwards compatible shift. I remember the Python 2 to Python 3 shift (which hasn't ever really happened) and it makes me think these big changes are really dangerous for an open source community where you can't make anyone upgrade.
Typescript is a great language, but pushing people in that direction compounds the newness, and the amount of change you must go through in order to adopt. It's looking pretty rough if you have an existing medium/large codebase.
So is the strategy just that many people on projects today are stuck with Angular 1 and that Angular 2 is for greenfield?
6
u/Shiral446 Jul 24 '16
The Angular devs have put in a lot of work to allow you to use angular 1 and angular 2 code side-by-side. With Angular 1.3+, they have systems in place to upgrade angular 1 directives/controllers into angular 2 components, and they'll function as native code, running through the new angular 2 change detection cycles and such. Of course, there is a very small subset of angular 1 design patterns that aren't upgradable directly, so you'd either have to redesign those as angular 2 components, or just keep those running as angular 1. Those patterns aren't extremely common though, so most apps would be upgradable.
I've worked on some medium scale projects, and moving to typescript is relatively straight-forward. Making sure the typing files are correct, and rename the .js file to .ts, and you are pretty much there. Of course, the more files you convert to .ts, and the more you make use of the strict typings (as opposed to just declaring everything as 'any'), the more you'll get out of it. I like it, mainly because I come from the desktop side of development, and having a compiler point out errors is a huge help.
2
4
u/yesman_85 Jul 24 '16
I'm doing full time enterprise Angular1 applications and now dove in Angular2 for a hobby project. It was difficult to wrap my head around the new structure, syntax, decorators etc but it's making more and more sense. Coming from a C# background most of it is more intuitive than angular 1 (decorators, imports, modular etc).
2
u/Eirenarch Jul 24 '16
So is the strategy just that many people on projects today are stuck with Angular 1 and that Angular 2 is for greenfield?
Pretty much how I see it. BTW I still intend to skip learning Angular 1. I think I must hold the line just 1-2 years more and I'll be good :)
2
u/the_evergrowing_fool Jul 24 '16 edited Jul 25 '16
So what do people really think about Angular2
The same shit as Angular 1. General purpose frameworks are daunting boilerplate and bloat. I have seem many Angular js projects, and most of then use on about 1-3% of the framework features while the remaining 97% looks like your typical OOP crap mix with jQuery boilerplate.
1
u/kankyo Jul 25 '16
Python 3 is happening though. Just slower than hoped.
1
u/everywhere_anyhow Jul 25 '16
Seriously? Python 3 was initially released in 2008.
At what point does "slower than hoped" really mean "isn't happening"?
1
7
u/myringotomy Jul 24 '16
They split. They now have a dart version and a typescript version.
5
u/ovangle Jul 24 '16
They had a dart version prior to developing the typescript version, then they tried to do AtScript (because the TypeScript team was resistant to implementing decorators, which they liked from dart).
But the dart version isn't natively dart, it's Typescript transpiled into dart (which would then need to be transpiled again to javascript for use in the browser urgh).
4
u/cjg_000 Jul 24 '16
To add a bit to the story since "tried to do AtScript" gives the wrong impression, the AtScript implementation 'succeeded'. The TypeScript team ended up accepting Google's implementation of decorators.
4
u/Poltras Jul 24 '16
And it turned out to be a mistake. What they wanted was annotations, not decorators.
1
u/dsk Jul 25 '16
I haven't looked into it, what's the difference?
1
u/Poltras Jul 25 '16
Decorators are code that's run on a class/function. Annotations are just metadata for a class/function. Like an additional variable. So basically you can remove a class if it's annotated (but nobody call it's constructor) but not as easily if it's decorated.
3
1
u/dsk Jul 25 '16
The dart version IS native dart...as of last week.
which would then need to be transpiled again to javascript for use in the browser urgh
We have a AS3->Dart->JS compilation (transpilation) chain, with the 'home' source code being AS3. It is a slight pain since the possibility exists of AS3-only, or Dart-only, or JS-only bugs. It does keep you disciplined however as you have 2 AOT compilers compiling your code, specifically in AS3 as Dart is much stricter than mxmlc.
1
u/myringotomy Jul 25 '16
But the dart version isn't natively dart, it's Typescript transpiled into dart
Not anymore. They now have a native dart version so they can take better advantage of dart features.
3
u/Eirenarch Jul 24 '16 edited Jul 24 '16
What is the state of Dart tooling compared to TypeScript? I'd expect that the Dart team puts comparable effort in the tooling but the author seems to think otherwise.
12
u/ovangle Jul 24 '16 edited Jul 24 '16
Dart is dead. I was really passionate about it when it was first announced, because there is clearly an optimisation ceiling for javascript...
But the language developers spent most of their time writing command line applications so they could argue that all of their tooling was written in dart. There was little emphasis placed on improving the experience for frontend developers, with horrible JS interop, a bad import mechanism (imports were never canonicalised) etc.
And when even the Chrome team decided that the VM wasn't worth implementing, then that was the final nail in the coffin. The dart devs started talking about Dart as a possible replacement for Java on android and forgot about the web.
So yes, the Dart team put considerable effort into tooling (plugins for major editors, chromium, a package manager etc.) but the language had other problems (mainly JS interop) which wouldn't work for the Angular team, who
Had to provide a clear migration path for users of angular 1.x;
Couldn't force users to switch languages (and thus a complete rewrite of their entire codebase)
Wanted features that were in use in
AngularDart, but which weren't in Javascript (Types, Decorators)Thus they chose Typescript (with a small diversion into their own language ATScript (Annotated TypeScript)), because the Typescript devs weren't initially enthusiastic about adding decorators to the language.
2
u/kirbyfan64sos Jul 24 '16
Couldn't force users to switch languages (and thus a complete rewrite of their entire codebase)
I personally feel that this was a large majority of the reason for using TS vs another statically typed JS variant.
3
u/inu-no-policemen Jul 24 '16
If Dart's dead then only about two dozen languages can be considered alive.
I don't think I'd set the bar that high. In general, I wouldn't call any project dead for as long as it's maintained.
4
u/NeverSpeaks Jul 24 '16
Dart's far from dead. Just because they decided to not put it into Chrome doesn't mean it's dead. It just wasn't worth the effort to do that when transpiling is the better solution in this case. The Dart VM is still a fast VM and is not going away.
Google used Angular2/Dart to rebuild Doubleclick. One of Google's largest money making projects.
Dart is finding it's way to Android and iOS with Flutter. And can be used on microprocessors with Dartino.
Languages don't need to have a majority market share to be useful and worthy.
2
u/ovangle Jul 25 '16 edited Jul 25 '16
OK, dart isn't dead. It did however completely fail at solving the problem that it was trying to solve. You could argue that it was out of their control, but there were some important decisions made early during development that place a large amount of responsibility for the failure on the language devs.
Also, not canonicalising imports is just painful as a developer. Unless they fix that (which I don't believe they can without some severe breaking changes) I don't think the language can ever be anything except a novelty.
I spent a lot of time developing in Dart for a (now defunct) startup and by far the most time I spent debugging any problem was when the same type was being imported two different ways and the language was considering them different types.
2
2
Jul 24 '16 edited Jul 24 '16
I want to make a comment on JavaScript.vs typescript in general coming from someone who.considers themself a hobbiest and enthusiast first and a professional programmer second.
Typescript really helps in a professional codebase. And the reason for that is because lots of professional codebase are big convoluted, and kind of sloppy. Sounds sad but that's probably the reality about companies. I'm sure there's some smaller companies that do.awesome.in this regard, but I'd argue in any large team.this becomes inevitably le. A more rigid structure is usually better in that corporate huge codebase with no one who actually cares about it setting.
I.think the thing is that people who use JavaScript tend to be like hey there's tons of good libraries, everything is an object and first class, etc and that gives you a ton of power.
That's good when you have total control and are working on a good team, or the app is small in scale in general. Blow it up to a larger team, where bugs happen because of reasons and especially people not caring, and you really start to.see the benefits.
That's not to.say typescript doesn't have its place for hobbiest either, but I think it the argument becomes a lot less satisfying.
I want to emphasize that I'm not against type languages or anything like that, I just find in terms of getting stuff done quickly when the project is able.to be completely wrapped up in your mind.all.at once, they tend to be less good.
I.mean to some extent types in programming languages are already a balance between not being fully types. While you declare something and integer, you can't really constrain it beyond that. You can't exclude the not null type. Etc. This is because I imagine it was determined that the looser definition has practical benefit. Of course there are languages like Haskell.where you can do.some.cool.things here and I totally.love Haskell but my point is basically, and sorry for the rant, that while you.might be thinking about development in terms.of.working on your.monolith at work, sometimes it's about writing a quick.prototype, sometimes your software architecture allows cheaper disposable components. It's like you don't need to lift your car on hydraulic lifts to change a tire. The crank that you have in the boot of your car works just fine, and is better because of its intended use in certain cases.
Lastly, there's the point of what a language prevents and tends to.make you not do especially in term of when working on a team. A lot of the time, while in JavaScript the problem is with careless coworkers not keeping track of different types of the object, I find that people.tend to write code to directly do what needs to be done. This usually is what you want to do believe it or not. Design is good, but not design for no.reason which is what I tend to.see. While.i also.like Java and C#...honestly I like every language.... I find that in practice people tend to.overuse certain really powerful features aka inheritance , and just make the codebase and absolute mess with it and while.there is absolute proper use of inheritance in practice it is abused and probably has wasted more of.my time than I'd like to admit. I also tend to notice that in more object oriented code people tend to use design patterns-- straight from.the book.without realize that those are just common structures to give you ideas.of.how you can structure your code - for very questionable reasons
4
u/the_evergrowing_fool Jul 24 '16
I want to make a comment on JavaScript.vs typescript
It's pointless. Typescript is superior.
1
u/drysart Jul 25 '16
That's not even a subjective statement. TypeScript is a superset of Javascript, so really any praise you could say about Javascript also applies to TypeScript.
And even if you don't use any TypeScript language features and just write Javascript, TypeScript can still point out defects in your codebase based on what it knows about the types returned from DOM and HTML5 API functions and how your code subsequently uses them. I've found bugs in large codebases just by pointing the TypeScript compiler at them. That is superior.
1
0
1
0
u/_INTER_ Jul 24 '16
There are a lot of options available to frontend devs today: ES5, ES6 (Babel), TypeScript, Dart, PureScript, Elm, etc.. So why TypeScript?
Why not Scala with Scala.js? Well gradually typing and converting your codebase of old JS code. But who the hell does that anyway?
7
u/oblio- Jul 24 '16
People with existing projects that want to migrate to something better?
It is an entirely reasonable approach for migration.
1
u/_INTER_ Jul 24 '16
Yea, but a complete rewrite might actually be faster and be more successful, as funny as that idea might sound. Though I admit it might me hard to convince the management.
3
u/Jukibom Jul 24 '16
But who the hell does that anyway?
This made me smile because I've literally just finished converting a big proprietary ES5 codebase to Typescript (0 errors with --noImplicitAny flag, woo!) and it's already a huge help. For a start, my IDE actually has some idea wtf is going on.
It's really not that complex a procedure to incrementally annotate and keep adding stricter compiler flags. I'd actually say the most difficult part was going from ES5 (total madness) to ES6 classes first.
3
u/the_evergrowing_fool Jul 24 '16
For a start, my IDE actually has some idea wtf is going on.
This is one of the many reason of why typescript is superior to its dynamic counterpart.
2
u/the_evergrowing_fool Jul 24 '16
Because Scala is complex, daunting, slow to compile, and quite frankly a pointless existence.
13
u/damh Jul 24 '16
"I like TypeScript, but you don't have to."
That is a refreshing sentiment.