神刀安全网

When should I use TypeScript?

Last summer we had to convert a huge code base (18,000+ LOC ) from Javascript to Typescript. During this transition I learned a lot about the differences and similarities of the two and what makes a good case for using Typescript and when it doesn’t make sense.

Use cases for Typescript

  1. Code size: When the source code is huge and more than one person works on the project, a type system can help avoid the obvious errors. When someone can change code that potentially can another one’s code, it’s generally good to have some sort of safety mechanism. The compiler of Typed languages reveals mistakes with the obvious syntax errors (but can’t eliminate the need for debugging of course). If the source code is not huge, it probably doesn’t make sense to make it longer by adding type annotations. I have converted 180+ files to JS to TS and in most cases it added roughly 30% to the code size.
  2. Previous baggage: When you come from a strongly typed language like C# and don’t really want to go all the way in Javascript, TS is a good alternative. Even though I recommend learning Javascript for real, there’s nothing preventing you from using Typescript without knowing Javascript. In fact Typescript is made by the same guy who made C# so the syntaxes are similar here and there. In my company we had a team of C# developers who were coding a sophisticated desktop application in C#/WPF (which is front end in the desktop world) and then they were tasked to join my web project (as full stack), so it was relatively a shorter path for them to learn Typescript for front end and leverage their C# knowledge for the backend.
  3. As a replacement for Babel: I like the new Microsoft, they are cooperative and help the ECMA 6 standard. The old Microsoft used to take something that is standard (for example Java) and add proprietary non-standard stuff to it (for example J++) and try to force developers pick one. Typescript is exactly the same thing to Javascript (in fact Microsoft did the same in 1996 using JScript which is one of the reasons web developers hate Microsoft). Anyway, since ECMA 6 is basically a subset of Typescript and the Typescript compiler generates ECMA 5 code, it’s practically possible to compile ECMA 6 code to ECMA 5 (just as Babel ) using the typescript compiler.
  4. Library/Framework: if you are using Angular 2 or a library that makes it particularly easy to use Typescript, go for it. Just know that even though Typescript can use all Javascript libraries out of the box, if you want good syntax errors, you need to add the type definitions for those libraries. Fortunately some nice guys at DefinitelyTyped have made a community driven repo and tooling for that. But that is one extra step when you’re setting up your project.

When not to use Typescript?

  1. Extra compilation tax: There are no plans to support Typescript natively for the browsers. This means you will always have to compile your code before feeding it to the browser. On the other hand when ECMA 6 is supported by most browsers , that extra compilation is not necessary which makes the code load. You just modify the file and refresh the browser. That’s all — no “watching” or “compiling on demand” or build system.
  2. Weird debugging edge cases: Sourcemaps make it easier to debug Typescript, but the status quo is not perfect and there are really annoying/confusing edge cases that the browser things you are running a certain line of code but you are not. Also there are some problems debugging the `this` keyword and whatever is attached to it occasionally.
  3. Performance penalty: in our project we had 9000+ lines of ECMA 5 Javascript that delivered pure horse power for a WebGL component but we kept it that way. Typescript compiler (just like Babel), generates code that no matter how good the compiler is, can’t surpass the optimisations of a good programmer. That being said, that performance penalty probably is negligible compared to benefits of a type system and more modern syntax for most projects, but there are cases that milliseconds and microseconds matter and in those cases compilation of any kind is not recommended (even Babel, CoffeeScript, etc.)

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » When should I use TypeScript?

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮