Needs advice
on
ES6ES6
and
TypeScriptTypeScript

I consider myself now (after a few years of practice) to be a decent JavaScript/Node.js practitioner. So can someone convince me that I have to learn TypeScript and use it in my everyday life? In other words, why would TypeScript be necessary over some proper ES6 compliant code with strict mode enabled? Any thoughts/opinions are welcome.

EDIT 07/20/2020 : Thank you all for your feedback. I'm definitely going to invest some time in some TypeScript education in the long run. Apart from all the points you made in your responses (static typing, compilation, codebase consistency, etc ...), the fact that Deno may go big (which I hope, the improvements over Node.js could be life changing) and that Visual Studio Code (which I use) is built on top of Electron using TypeScript is what convinces me.

READ LESS
mulekick · GitHub (github.com)
17 upvotes·31.1K views
Replies (8)
Recommends
on
TypeScriptTypeScript

I am a big advocate for TypeScript. It even became a job requirement for me. I would probably refuse to work on a JavaScript codebase again.

Just to add to the other pieces of advice:

1 - Compile-time errors

Typed languages give you compile-time errors on top of runtime errors. If your code compiles it means you already have cleared a few bugs before you even start running your app.

2 - Documentation

Having typed properly your code gives your documentation. You know what is the type that any variables hold, You know what is the return type of every function... With pure JavaScript, you often have to rely on naming conventions. With TypeScript, you can enter any codebase and straight away know the type of variables, the shape of objects, etc...

3 - Auto-completion

Types give you auto-completion. Using a third-party library or accessing properties of an object, your IDE, will give you all the possible options and will moan (compile-time error) if you try to access a property that is not defined or try to map over an object for example.

4 - Let you learn other languages more rapidly

As a JavaScript developer, it is often impossible to read a codebase of a different type-language (java, swift...) and understand. Understanding types will allow you to (more) rapidly understand other programming languages.

TypeScript is not perfect in my opinion as it is not part of the JavaScript language, and you often struggle in trying to stop Typescript "complaining". But the pros largely overweight the cons in my opinion.

One important thing in setting up TypeScript is making sure you enable strict rules. Such as "strictNullChecks", "noImplicitAny", "strictFunctionTypes". If you TypeScript configuration is too permissive, you might be ending up writing more code for not that much safety. Basically you want to catch as many errors as you can in compile-time and only run the app once you feel confident it should work.

READ MORE
3 upvotes·3.7K views
Technology Leader & Software Engineer at Minna Technologies·
Recommends
on
TypeScriptTypeScript

I was asking myself this same question a few years ago. I felt that ES6 provided a terse, declarative syntax, that in combination with unit tests was reliable enough. Dynamic typing kept the code looking clean and left most functions generic and reusable for several data types. However, as your code base starts to grow and more and more "ad hoc" data types are introduced. It starts to become very hard to maintain and refactor code because you have to keep long call chains in your head and you sometimes don't know if you can remove a field from an object without introducing a runtime error. In essence, the less strict a programming language is the more you will have to test explicitly all the time. On top of that JavaScript is really good at coercing types in weird ways.

So the main benefit that TypeScript brings is static typing. You make your code more explicit, thereby limiting the number of unintentional behaviors that are possible. The compilers type checker can suddenly take care of testing these extra constraints for you and you start removing this burden from your unit tests. As a matter of fact TypeScript has a pretty advanced type system, a magnitude better than the current Java version (14) or Go-lang. One of these features are union types (aka. coproduct, sum type, etc). This lets you represent your types as "this thing" or "this other thing". Returning a disjoint union can make your code even more explicit and thus further limiting the number of unintentional behaviors. A simple example of this is the head function that returns the first element of a list. Either you get the value or you get undefined if the list is empty. A better approach is to return a Maybe type which is either an instance of Just(<value>) or Nothing. This tells you that the code calling head needs to handle exactly two possible cases. Languages with even stronger type systems like Elm and Haskell can even enforce that your code won't compile unless you handle all the possible cases for any union type. So again the compiler helps you avoid more runtime errors.

So to conclude the benefits of using a language with strong static typing:

  • Compiler helps you avoid runtime errors
  • Substantially reduce number of explicit unit tests
  • Better security and reliability by preventing unintentional behavior
  • Let's you refactor code with confidence
  • Code becomes more self-documenting
READ MORE
22 upvotes·4 comments·14.4K views
Ashwani Agarwal
Ashwani Agarwal
·
July 16th 2020 at 9:12AM

The unintended behaviour of addition (or not knowing the gotchas) on web-browser have always got FE devs scratching their head. I never really got a chance to work on an actual TS project professionally, but having worked with Node.js quite a lot and later moving to Java (mostly for web services), I just didn't want to move back to Node.js. But TS is definitely a game-changer.

·
Reply
Chris Sundberg
Chris Sundberg
·
July 16th 2020 at 9:45AM

Unfortunately there is too much "quick and dirty" rather than "correct, secure and reliable" in our industry.

·
Reply
Babbu 037
Babbu 037
·
April 1st 2024 at 2:35PM

Official Eric Emanuel Store Shop ‎EE® Shorts Eric Official Clothing from Official Eric Store Fast Delivery Worldwide 24 7 Customer Support

https://eestore.shop

·
Reply
Babbu 037
Babbu 037
·
April 1st 2024 at 2:35PM

Official Eric Emanuel Store Shop ‎EE® Shorts Eric Official Clothing from Official Eric Store Fast Delivery Worldwide 24 7 Customer Support

https://eestore.shop

·
Reply
View all (8)
Avatar of Kevin Amiranoff