-3

I am wondering about using TypeScript as a way to write my code for a project that is written purely in JavaScript. The benefits that comes from using TypeScript has grown on me significantly. For now the project is a AngularJS 1.5 project that will be migrated into the near future. I would like just some advise since I am joining the project and wanted to know the pros and cons of doing such a thing.

To help you understand my approach I will list below:

  • Build a local environment that allows me to run TypeScript locally.
  • Convert a small feature from the existing project into TypeScript and use a base.
    • Transpile the .ts file into JavaScript and then replace the feature code with the new .ts code transpiled into JavaScript.

What would be problematic about this?
What would be beneficial?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
crisam
  • 53
  • 9
  • were you you afraid of what might happen if you just go and put in a part of the build process and just start doing TS instead of JS for this part aside form just coding and building or were you asking for technical recommendations? – Robert Mennell Jun 20 '18 at 23:15

2 Answers2

4

TypeScript is a superset of JavaScript. All you need to do is rename your existing JavaScript file to .ts and you can start refactoring!

The default settings of the TypeScript compiler will compile "in place". That is, when you run the compiler it will output a JavaScript file with the same name in the same place, so everything should work exactly the same.

Benefits:

  • Type safety
  • Safe lambdas
  • Many, many others

Con:

  • Have to set up/run typescript compiler
  • Generated JavaScript is difficult to read/debug (need source maps for the latter)

Note that this is different than setting it up to run Angular 2+ (you may have just mistagged). Doing that piecemeal will be far more difficult.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • yea I agree but for now it would help with staying with the strongly typed mindset while i figure out the codebase – crisam Jun 20 '18 at 18:09
  • 1
    @crisam err... ok? Was there a question/comment there or were you just agreeing with my answer? – BradleyDotNET Jun 20 '18 at 18:12
  • I think a huge "con" part that the quality of the generated js will be far, far away what you could have written by hand. – peterh Jun 21 '18 at 01:21
  • @peterh That's fairly opinionated. The generated JS will certainly be *different*, and not terribly human-readable, but that's true of normally minified code as well. Given that the generated JS isn't intended to be read... I'm not sure its all that important (and at least in my view, doesn't remotely cancel out the benefits!) I'll add it though – BradleyDotNET Jun 21 '18 at 01:28
  • @BradleyDotNET Minification is another problem. Comple the ts to js, then beautify it with js-beautify. It is simply... well... I think just the type checking simply doesn't worth that quality decrease. – peterh Jun 21 '18 at 01:32
  • @peterh We'll have to agree to disagree :). There's a lot more than just the type checking to TS as well. – BradleyDotNET Jun 21 '18 at 04:00
1

This could cause some problems for a few reasons, but first we should look at the decision that made the projects language JavaScript in the first place. JavaScript was probably chosen because it's a language known well enough by the team so that anyone can maintain it on short notice.

TypeScript, while gaining popularity, requires a build pattern and patterns which are not known to most developers (yet) and trans-piles to JavaScript since it's a superset. This means anyone maintaining your piece of code after you're gone would need to learn the TypeScript build process and idiosyncrasies before they could maintain it and if this is not common knowledge to your team could actually just be causing decreased maintainability.

It's the same reason CTOs tell people to do projects in a language they may not want to: in the long run the person who might be maintaining the code might not be you.

This is probably the biggest reason why you wouldn't want to. Sure it means anyone could LEARN, but it also relies on TypeScript's continued support (which for the foreseeable future is solid, but always think of worse case), and even worse delays immediate response to any bugs or breaking compatibility in your code that may need to be addressed on a short notice.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Robert Mennell
  • 1,954
  • 11
  • 24
  • 1
    If the build process isn't more or less automatic; you did it wrong. Your point is fair however. Also TS is supported by the two biggest software companies in the world. Unless they decide to just give up on it for some reason, support will be around for a while. – BradleyDotNET Jun 20 '18 at 18:40
  • As I would hope, but you never know. You can never design or program to the best case and must always account for the worst case sadly. – Robert Mennell Jun 20 '18 at 18:46
  • clarified a bit more that they would also have to learn typescript alongside the build process. Since after all if the build process breaks they ahve to learn it – Robert Mennell Jun 20 '18 at 18:49