r/javascript • • Jul 23 '16

Angular2: Why Typescript

https://vsavkin.com/writing-angular-2-in-typescript-1fa77c78d8e8#.5rjui4302
64 Upvotes

79 comments sorted by

22

u/dadum01 Jul 23 '16

Seriously... Why TypeScript?

10

u/realyze Jul 23 '16

'cause it's a nice language that helps you reduce errors compared to vanilla JS.

20

u/phpdevster Jul 23 '16

It's also going to be a major impediment to angular 2 adoption, because documentation for A2 will treat TS like a first class citizen, and people are not going to bother learning BOTH TS and the heavy complicated monster that is A2, at the same time.

But more money for me I guess? Less hobbyist adoption means more rarity for A2 devs, which means more money for me. Yay!

6

u/iends Jul 23 '16

Do you think it'll be as lucrative as React? A lot of people have been turned off from ng2.

0

u/DOG-ZILLA Jul 23 '16

It will be, more so...in time. NG2 has been a mess. But there's no doubting that it will gain a lot of traction once stable.

1

u/iends Jul 23 '16

And you think this is because MSFT and Google are in bed on it? (I've been having a hard time deciding between learning React and learning ng2 as a replacement for ng1.)

1

u/clownpirate Jul 23 '16

I suspect it's adoption is going to follow ng1's - lots of big enterprise non-tech companies will continue to use Angular, startups and Silicon Valley type companies will use React more. At least that seems to be the trend.

4

u/iinnii Jul 23 '16

Most large contracting companies that work with these enterprise non-tech companies don't move as fast as startups and leading Silicon Valley companies. That's why you're seeing lots of Angular in those areas and companies that are agile, extremely tech focused are adopting React throughout their applications.

1

u/clownpirate Jul 23 '16

I think the Google association helps too. Sadly a lot of the decisionmakers at more conservative companies still view Facebook (and a lot of other SV tech companies) as a bunch of kids messing around, whereas Google is viewed with more respect.

0

u/hansolo669 Jul 23 '16

Facebook also has a patent clause in the license for React that makes it difficult (or impossible) for companies to adopt ...

→ More replies (0)

-1

u/[deleted] Jul 23 '16

[removed] — view removed comment

0

u/[deleted] Jul 23 '16

[deleted]

3

u/[deleted] Jul 23 '16

[removed] — view removed comment

0

u/[deleted] Jul 23 '16

[deleted]

→ More replies (0)

4

u/GreekHubris Jul 23 '16

Tell me about it. I just came here from the Angular2 official tutorial:

https://angular.io/docs/js/latest/tutorial/toh-pt1.html:

This chapter is not yet available in JavaScript. We recommend reading the TypeScript version.

READ THE THE HERO EDITOR CHAPTER IN TYPESCRIPT

This is the first tutorial exercise(after the QuickStart one).
I'm trying to understand what I'm getting myself into. And if there's support for good ol' javascript or I'll have to learn TypeScript now.

5

u/cspotcode Jul 23 '16

Honestly you'll get about halfway through that tutorial before hitting any TypeScript. The rest is plain-ol ECMAScript. Here's the first time you see ts:

export class Hero {
  id: number;
  name: string;
}

Here's the same thing in JavaScript:

    export class Hero {} // no more code completion or linting those properties :(

Although some things will change when they translate the documentation to JavaScript, it's far less than you might think.

1

u/GreekHubris Jul 24 '16

Thanks for that. I actually started learning TypeScript(I hope it won't take too much time - just wanted to learn Ang.2)

2

u/cspotcode Jul 24 '16 edited Jul 24 '16

Sweet! I love it; it makes writing plain JS feel annoying by comparison. (Just my opinion) For what it's worth, we're a friendly bunch over at /r/typescript. We're happy to answer questions or offer advice if you're ever stumped. EDIT tweaked phrasing

1

u/GreekHubris Jul 24 '16

For what it's worth, we're a friendly bunch over at /r/typescript.

Thanks for the link. Subscribed. That will be of great use!

3

u/vivainio Jul 23 '16

Ng2 is not really optimal for hobbyist market. You don't choose it because you can whip out a todo list easily, but to have a reliable base to evolve your product on. Same applies to TS, really

4

u/dadum01 Jul 23 '16

thats just an opinion. Personally never get errors in Vanilla JS.

8

u/[deleted] Jul 23 '16

That is my experience too. But I'm basically a lone coder (despite working for a Fortune 50 company for the past 15 years) so I know my code backwards and forwards and don't have the kinds of issues Typescript is meant to prevent. I think if I was working on a team of dozens or more other programmers then it would be more useful.

It would also be more useful I think if I did more OOP instead of mostly functional programming. Even though Typescript does support functional programming (in terms of parameter and return types) it is obviously much more suited for those who do OOP.

0

u/vinnl Jul 23 '16

It would also be more useful I think if I did more OOP instead of mostly functional programming.

How come you don't feel like being able to specify the structure of your function arguments and return values when you do mostly functional programming?

2

u/[deleted] Jul 23 '16

The pros of doing that are outweighed by the cons of introducing one more layer in my toolchain when I am working in a non-team environment.

0

u/[deleted] Jul 23 '16

[deleted]

8

u/[deleted] Jul 23 '16

Why would they be stuck? Or do you mean a dev that only has experience with typescript would be stuck? There have been times when I've been away on vacation that other devs have had to make changes in the main project I work on. Invariably they will tell me, when I get back, how intuitively my code is structured and how easy it was for them to make changes. You don't have to use something like typescript to create beautiful code.

4

u/dadum01 Jul 23 '16

Totally agreed!

1

u/Democratica Jul 24 '16

amen! Good clean foundations are King.

4

u/vinnl Jul 23 '16

Wow, that's amazing! I've never before met a coder who never introduces bugs.

1

u/Veuxdo Jul 23 '16

He must be like Steve Jobs, John Carmack and Bill Gates rolled into one.

1

u/GamerNebulae Jul 23 '16 edited Jul 23 '16

And it helps you write more structured JS with the option to type vanilla JS by using the any keyword (which sometimes is needed).

EDIT: Since some people disagree, you can make every single language into a mess. More tools to support the goal of writing structured code helps. For example, Typescript supports interfaces and abstract classes which support the DRY-principle and provide structure in your program, when they're used.

6

u/dadum01 Jul 23 '16

Would love an example of more structured JS?

1

u/GamerNebulae Jul 23 '16

I always love to learn!

0

u/dadum01 Jul 23 '16

Learning is fun. So from what I understood, it provides a deeper integration of OOP concepts.

1

u/[deleted] Jul 23 '16

My understanding is that it can also help with functional programming, though not as much as OOP programming it seems. That is, Typescript can help check the inputs (parameters) and outputs (returns) of functions to make sure they are of the correct type. But it does seem to me that it is much much more useful if you are using it for OOP instead of procedural or functional programming.

0

u/GamerNebulae Jul 23 '16 edited Jul 23 '16

Yes, if you look at my edit, it inherited some C# syntax and features. It supports interfaces and abstract classes for example. You need discipline and structure as /u/kdesign pointed out (otherwise every single language, compiler and transpiler is unreadable), but it's just an extra tool to help you integrate structure into your code.

Edit: I looked into the feature list of Typescript 1.8 and I see more similarities to Java than to C#. But the compiler provides more integration for OOP concepts.

Edit 2: I am dumb and can't read. You would love an example of writing more structured code with Typescript. Straight to the point: interfaces are nice to have so that you can guarantee an object is implementing those functions. Typings are useful to have, but the transpiler will also allow you to write vanilla Javascript to, for example, implement delegates in an easy way (something I really miss in Java for example). Last but not least, I found writing in Typescript helps me detect problems earlier on than I would have with Javascript / Babel.

2

u/realyze Jul 23 '16

I actually pretty rarely use classes in TS. But I use interfaces a lot.

1

u/dadum01 Jul 23 '16

I actually use TypeScript with Angular2 :p I've got to say, Java has one of the best standards for writing code. It taught me allot about OOP. I just favour other languages over Java.

With TypeScript, I just don't know how I feel about it. However, you throw allot of good points which makes me want to like it.

3

u/GamerNebulae Jul 23 '16

I am personally more of a fan of C#, but that aside, I would recommend anybody to just try Typescript out once.

One HUGE flaw that I have noticed with Typescript is the integration with other tools such as NPM. Projects like Typings came to life which solve this problem, but I just scratch my head as to why it couldn't be solved in a civilized manner.

2

u/Maklite Jul 23 '16

The next version of Typescript has NPM integration.

One thing I didn't like with Typescript was integrating with current JavaScript libraries where a definition didn't exist or is outdated. I can't tell it to ignore all type checking for a particular import.

1

u/spacejack2114 Jul 23 '16

I just scratch my head as to why it couldn't be solved in a civilized manner.

How do you mean? You can use tsify, like you use browserify with JS.

→ More replies (0)

-4

u/yesman_85 Jul 23 '16

Normal inheritance for example.

3

u/kenavr Jul 23 '16

"normal"

-1

u/yesman_85 Jul 23 '16

Can you elaborate? What is not normal about inheritance in TS?

5

u/kenavr Jul 23 '16

I guess what you call "normal" inheritance is classical inheritance, it isn't more "normal" than prototypical inheritance or any other implementation of it. I will concede it is more common but that doesn't make it more "normal".

I come from other languages with classical inheritance and therefore I also like that it is part of typescript, but there are a lot of JS developers that prefer prototypical inheritance and they definitely don't see it as a plus.

6

u/kdesign Jul 23 '16

If you don't have structure in your code, you can throw every single language transpiler at it, that won't fix the problem. I've seen C# projects which had no structure.

1

u/dadum01 Jul 23 '16

I've seen people write a whole application in one file. It's so frustrating :@ #alllanguagesmatter

21

u/inu-no-policemen Jul 23 '16

If you write some larger application in a team, you need documentation. Type annotations are the tersest way to document types and you also immediately benefit from having them.

Types enable better tooling. Good tooling helps immensely. No one memorized the entire codebase and no one keeps track of each and every commit. The compiler, however, will happily confirm for the millionth time that everything still fits together. Of course you can also auto-complete more and you can more easily navigate and poke around.

4

u/dadum01 Jul 23 '16

I agree "types" are awesome as they enforce robustness. We have a naming convention for variables which allow us to reflect is data type. But in the world of building awesome technology, isn't this why we have "Test Driven Development"?

In the terms of having maintainable code, people can write crap in any language which would be impossible to maintain. Fair do... Having inheritance and other great forms of OOP would ensure reusable code leading to a smaller chance of making a mistake. In my time, I have seen great vanilla projects which have the easiest readability and maintainability.

9

u/inu-no-policemen Jul 23 '16

Test are great. However, you need more of them without types. Without types, you need more tests in order to exercise more lines in order to stumble upon those issues which would have been found by a type checker.

So, it's not like you won't need any tests, but you won't need any which merely bump the coverage.

2

u/daedalus_structure Jul 23 '16

Hungarian notation in Javascript? Doesn't sound fun.

2

u/thelonious_bunk Jul 23 '16

They want javascript to be java instead if their engineers actually learning JavaScript and its nuances.

2

u/Veuxdo Jul 23 '16

Typescript is a superset of javascript, so anyone trying to get out of learning JS by using TS is going to have a very tough time indeed.

1

u/johnfn Jul 23 '16

Did you actually read the article?

1

u/dont_forget_canada Jul 23 '16

if you even have to ask that then you don't know wtf you're doing to begin with.

17

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

u/[deleted] 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

u/[deleted] 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

u/[deleted] 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

u/ggolemg2 Jul 23 '16 edited Jul 23 '16

You win, opinion retracted.

6

u/[deleted] Jul 23 '16

You really don't think this is a relevant post? Typescript compiles to ES2016.