Hacker News new | ask | show | jobs
by OmarIsmail 3896 days ago
Once you go typed JS you don't go back.

We had a large existing pure JS codebase, so Facebook's Flow was a better fit for us. We still have some portions of our code that don't have type annotations, and invariably that's where the majority of new bugs are introduced. Now we have a policy of making sure all the files we touch are typed, and adding types to a file if it doesn't already have it.

Types + React is a whole new ballgame when it comes to front end dev.

1 comments

Every JS code is a valid TS, since TS is a superset of JS.

You can benefit immoderately from compiling your existing codebase in TS; you'll probably discover some bugs, even before adding any annotations.

> Every JS code is a valid TS, since TS is a superset of JS.

There are still cases where you'll need to sprinkle <any>. For example:

    var state = { foo: 1 };
    if(something) {
      state.bar = 2;
    }
is valid JS, but the TS compiler will complain that `state` does not have a member named `bar`.
The TS compiler will complain but still generate working JS code, so you don't lose anything.
But the compiler's output will be polluted with these false positives, making it harder to see actual errors. (Also, there's a compiler flag to prevent codegen on error, which comes in handy sometimes.)
We made the decision some months ago and Flow's ES6 support was better than TypeScript's (and we already have a lot of ES6 code).

We also have an established toolchain with gulp and browserify and babel, and again, at the time TS didn't play as nicely (vs Flow which just worked). Things are definitely improving in the TS world, and I keep tabs on it. The fortunate thing is that both Flow and TS' annotations are compatible, so it should be relatively easy to switch from one to another.

Whichever one you go with doesn't actually matter though. As long as you go with one of them you'll see a massive increase in productivity vs vanilla JS.