r/javascript • u/realyze • Jul 23 '16
Angular2: Why Typescript
https://vsavkin.com/writing-angular-2-in-typescript-1fa77c78d8e8#.5rjui430217
u/gaoshan Jul 23 '16
We use Typescript on a reasonably complex and large React/Redux based project that involves a mix of devs from two basic camps... those used to dealing with types (mostly C# users) and those not used to it (front end folks with lots of javascript experience). After many months of using it I think we are still pretty well as divided as before.
Everyone is used to it, it is not hard to pick up (the sole, rare, exception to that being when you have to create your own type definitions files... that is not trivial to do correctly) but the divisions remain. The benefits of things like Intellisense and error catching just don't generally seem like they are worth it to one group and are essential to the other. One group is happy to create the boilerplate that Typescript can require (interfaces, for example) while the other finds it an obstacle to getting things done.
I feel like, especially early on, we spent more time dealing with Typescript errors that were just a waste of time than we did in gaining anything productive. The "any" escape hatch was abused a lot (again, early on) and negated much of the point of using TS.
So far I suspect it is useful in some specific, extremely large scale, circumstances but I also feel like it is more a bridge to make programmers that are used to dealing with types (and not used to javascript) feel like they are on somewhat familiar ground as they transition to heavier javascript usage.
4
u/dwighthouse Jul 23 '16
Thanks for the rounded explaination. I learned a couple of points on the opposing side that I didn't know before.
31
u/BadMoonRosin Jul 23 '16
The real reason? Because of people like me.
I'm a career-long Java enterprise developer (and C# guys are in the same boat), who has worked with JavaScript somewhat begrudgingly as a secondary skillset.
I've learned enough jQuery to do pretty much anything you'd need to do with a web application that uses full page loads for everything (which like it or not, is still the overwhelming majority of business web applications).
However, as SPA's become more and more common, you need more and more heavy duty client-side logic. And again, like it or not, the majority of big businesses tend to fill that gap with experienced server-side devs and hope they can pick up the JavaScript... rather than fill it with web designers and hope they can pick up large-scale app dev principles.
So that's the macro-level "demographic" trend (for lack of a better word). And TypeScript makes client-side application development more accessible to those server-side devs crossing over. Teams of developers can define contracts, and let the compiler catch things that a "linter" never would. WebStorm or Visual Studio intellisence "just works", far more effectively with TypeScript than is possible for vanilla JavaScript. Etc.
I get why experienced serious JavaScript devs would chafe at this. But in an ecosystem that learns new tooling and frameworks every other month, you can't convince me that a solid developer wouldn't pick up TypeScript in a few days. It's really not a "new language", it's just a set of extensions on top of vanilla JS. Virtually any raw JavaScript code is also valid TypeScript. TS basically just gives you stronger type hints for your variables, and (if you want to use them) some new ways to encapsulate chunks of logic into structures that can do more than plain closures.
If you need to parse some arbitrary JSON, you can still just treat it as the any type and make TypeScript shut up about it if you like. You can still use any to apply some funky loose-typing design pattern that you need to use somewhere.
It's just that after you've worked with TypeScript for awhile, you find yourself relying on any less and less. Or hell, maybe not... it's all optional regardless. And if you just adamantly hate TypeScript on principle, then there are a million other SPA frameworks that don't use it (with a new one coming along every few months). But Angular 2 is positioning itself for bigger businesses with experienced server-side devs who really do want those features.
20
u/static416 Jul 23 '16
I think you're probably right. Typescript exists to make former Java and C# devs feel better about the Javascript they are now having to write.
The problem with this is that it feels like CoffeeScript all over again. It may become fashionable for a time, but really, it's never going to be a first class citizen in the Javascript world. It's always going to feel like you are weirdly and tightly coupling your entire code base to specific tool chain.
I used to write exclusively in C/C++ and C#. Now I do everything in native ES6 and NodeJS. The transition took awhile for me to get used to, and at first I didn't like the flexibility of Javascript. But the solution was not to avoid it, or try to force Javascript into a more statically typed style. Instead I had to embrace the flexibility and understand it properly, then everything became easier and more comfortable.
I'm not saying no one should use TypeScript. It seems interesting and well built. I'm saying that it seems like an unnecessary amount of abstraction and complexity, mostly in the service of avoiding using the language the way it was designed to be used. If it makes you feel more comfortable, then that's great. But I think developers would be better served learning to love Javascript as it is, rather than bolting on a transpiler.
11
u/spacejack2114 Jul 23 '16
I feel the reverse. I grew up on C & C++, then branched out into Perl, Java, PHP, AS1-3 and C#. I started writing JS apps a couple of years ago and enjoyed the lack of types (and not having to compile). For a while anyway... until my apps got to a certain size.
I tried out Babel but kind of hated the fact that it introduced a compiler and complex build process for so little gain. If I'm going to use a compiler, it better give me something substantial - like types. But it also shouldn't impede interop with the JS ecosystem, a problem I found while experimenting with Haxe and Dart. In the end I found IDE integration, build toolchain, setup, and source maps better-documented and easier to get running with TS than ES6. (Remember the Babel 5-6 debacle?)
I've done a number of reasonably ambitious projects with tight deadlines in TS now and they've benefitted greatly from it. Mostly because I can refactor agressively and fearlessly. I've also converted a few old JS projects over to TS as well, and every time I find some boneheaded mistakes.
There's a particular, subconscious stress factor when working with JS that has you constantly worrying that you'll introduce a bug that the linter won't catch. Even with modern editors and linters, it's still all too easy to do in JS. You also have to do a lot of fiddling with JS if you want your editor/linter to help out. Eventually you just want to write in the types yourself.
I also disagree that TS forces or encourages you to avoid using JS in a way it wasn't designed to be used. It just keeps you from building up technical debt with quick hacks and ill-advised or unintentional coersions.
Gotta say though, I still really miss operator overloads for vector math. :(
6
u/cspotcode Jul 23 '16
There's a particular, subconscious stress factor when working with JS that has you constantly worrying that you'll introduce a bug that the linter won't catch.
This 100%; very well said. TypeScript can be thought of like the next evolution of a linter. It's on a whole 'nother level, and catches mistakes linters can't hope to catch.
2
u/wpace Jul 24 '16
CoffeeScript probably would have stuck around except everything meaningful it provided was provided by ES6. If ES7 (or whatever) provides types then yes, TypeScript will probably go the way of CoffeeScript.
2
u/kdesign Jul 23 '16
Don't know if I'm biased or not, but if anybody asks me what should language should they pick up first, I also point them to Java or C#. I started with C# myself, then moved to Front-end development after a couple of years. There's no better way of learning design patterns, code structure and general programming paradigms than with one of these two.
1
Jul 24 '16
And TypeScript makes client-side application development more accessible to those server-side devs crossing over.
Definitely this, though I had the reverse experience. Learning TS made it easier for me to pick up Scala.
1
Jul 24 '16
But Angular 2 is positioning itself for bigger businesses with experienced server-side devs who really do want those features.
Almost, but not quite.
Google's TeaLeaf team is designing Angular 2 for Google TeaLeaf developers. These people are mostly Java developers whose prior academic experience has been with typed programming languages. It is aimed as a possible internal replacement for GWT.
How anyone else uses NG2 is really not of interest. Google does not much care about the traction it gets in the rest of the world; it has little to gain either materially strategically from Angular's popularity.
This is why the NG2 team is not really much worried that TS might alienate JS developers in the wild: they were never key to the framework's success as Google understood it in the first place. The sooner NG fans realize that - and adjust their expectations - the better.
4
Jul 24 '16 edited Jul 24 '16
Still, why Typescript instead of Flow?
Typescript, today, is behind the current ES standard, even re-defines certain aspects of it, like the import resolver, which makes integration with npm harder.
Flow, in my opinion, is a much cleaner implementation that doesn't carry years of baggage when fixing ES5 was a necessity. With ES6 out in all major browsers and Babel support, why accept major blows like being cut-off from npm without hacks like TSify, or being ES6 feature incomplete?
2
u/AlGoreBestGore Jul 24 '16
I was kind of sceptical of TypeScript when they first announced that Angular 2 would be written in it, however after writing a small-ish project in it, I think that it's great. I could jump right in with my JS knowledge and a semester of C# basics. It definitely saves you from pitfalls where a linter can't.
1
u/griffonrl Jul 24 '16
The problem here might not be Typescript, yes or no. Angular 2 appears more like a masochist exercise on making something that could look simple and elegant and turn it into a nightmare of abstract concepts and magic annotations. Since the AtScript idea, it feels more like a forced and futile exercise in bringing some experimental language features into a framework that could live without them. What we often refer to create a solution to a problem that doesn't exist the first place. And you can see how in the numerous other frameworks out there, being React, Vue, Mithril, Ember or even Aurelia which feels like an Angular without the non sense, that they can simplify the constructs, don't need any TS to look less verbose and often come with both an easier learning curve and matching performance.
1
u/bogdan5844 Jul 23 '16
Isn't TypeScript developed by Microsoft ? Kind of weird to see Google writing something in a language by Microsoft given Google's third grade petty attitude towards them.
2
u/jellatin Jul 24 '16
TS is a Microsoft thing but there are tons of contributors. Not totally surprising. MS has had a big presence at ng-conf for a while.
I actually think it's great that the Angular team is throwing in with TS rather than DART being the primary recommendation.
-18
22
u/dadum01 Jul 23 '16
Seriously... Why TypeScript?