How we failed, then succeeded, at migrating to TypeScript
heap.io
heap.io
Reading my old code, I was surprised by how clean it looks. How easy it is to digest. There is a certain sense of calm when your brain doesn't have to process all the visual clutter of a C-style syntax. I miss that.
I wish I didn't have to choose between CoffeeScript and TypeScript.
TypeScript first and foremost is about type safety and tooling. Its architecture is largely syntax-agnostic. It operates on the AST, so the parser and code generator can be swapped for a different syntax.
CoffeeScript is all about syntax and does not (and should not) concern itself with most of the semantics.
They could theoretically be used together if TypeScript simply allowed custom parsers/generators/formatters to be plugged in.
This would work with ESLint and other JS tooling as well. I did a POC on that a few years ago [0].
[0] https://github.com/gkz/LiveScript/issues/821#issuecomment-18...
I’ve not had this experience. I’ve found the ambiguity in coffeescript a maddening adventure in syntax confusion.
- function call syntax doesn’t need parens except if a 0 parameter method
- commas are largely optional both in arrays and function parameters. How do you parse: [f, f(), f a b c]
- implicit returns are a terrible idea.
- the @ syntax refers to either a ‘static’ method or an instance variable depending on the context.
- I have to do a double take for the object literal syntax every time
> function call syntax doesn’t need parens except if a 0 parameter method
Function call syntax doesn't require parens so to make multi-line calls punctuation-free. Especially if some args are objects.
> commas are largely optional both in arrays and function parameters. How do you parse: [f, f(), f a b c]
Well commas are not optional.
> implicit returns are a terrible idea
They're especially elegant in writing declarative code. It takes some getting used to though.
> the @ syntax refers to either a ‘static’ method or an instance variable depending on the context.
Agreed.
These are all tradeoffs though. For me, they struck the right balance between ambiguity (very little) and expressiveness.
LiveScript in comparison was much more sugary (which I preferred): https://livescript.net/
Well, guess it's Reason for me now :D
Reading other people's CoffeeScript on the other hand...
I really miss the elegance of Coffeescript. ES6 is great, Typescript is great, but sometimes I feel like I have to visually parse a lot of static cruft to read the essence of the program.
What if one's editor had a toggle hotkey to hide typescript typings. Maybe you could turn it off to visually parse the program real quick, before turning it back on to edit it.
Hmm...
When it comes to experimentation, I love Javascript and Python because of how fast they are to write when you're not thinking hard about types.
Then it's just discipline to know how to promote experiments to production code. TypeScript is beautiful because you can just turn it on and add them in. Same with Python type hints.
TS is what happens when you let .net devs write JS.
Let's see how that turns out in the long run.
Indeed, Coffeescript is one of the finest languages I wrote in. Consider that 95% of the time we are reading code, what is the win of TS with a linter? The TS codebases I've worked on really hurt my eyes and brain, and that for some stupid website where type safety hardly makes any difference at all. And all those average web developers I've worked with bragging about type safety while the code they are producing is full of wrong constructs, bad naming, dependent on heavy tooling, etc..
Key insight. It’s always people first, code is a far distant second. Love Kent Beck’s series on this, so insightful https://medium.com/@kentbeck_7670/software-design-is-human-r...
Great read, Heap team! Thanks for sharing :-D
I'm afraid that's kinda political. In order to get the migration successful, hmm.. I assume developers did not have much of a choice, either accept it or leave. You can try to brainwash them by impose your subjective point of view in a friendly way, but in the end it's all about power, that's the untold story.
But if compiled languages like C++ or Go had as many dedicated libraries for webserver management as JavaScript, would there really be a benefit to using Typescript?
I've been a bit traumatized by C++ so I can't help but see this as a bad thing.
A lot of the insanity in C++ is because template metaprogramming made a lot of micro-optimizations possible. You can use CRTP to achieve static polymorphism. You can use SFINAE for tag dispatch to choose a different algorithm at compile time. You can even fold entire algorithms down to a constant with constexpr.
This is great if you care about low level control of your code, but this is usually premature optimization in the JS world.
Luckily typescript will be immune to this because the types can't affect runtime at all. So far I haven't seen any truly monstrous generics that are so prevalent in C++.
This isn't at all close to CRTP or SFINAE, not just because TS/JS lacks the dispatch features necessary, but because in both cases you link it's still just about type declaration. CRTP and SFINAE are both ways to hack the type system to _run_ differently.
You can get cpp programs down to seconds for incremental builds with good management while having compile time checking.
(WASM and WAPI might eventually let you run WASM-compiled C++ in the client, but there's still a lot up in the air for interop purposes there.)
Any tips/thoughts??
I’d switch and I wouldn’t look back. I did switch my focus of learning and use, albeit from Ruby and Kotlin (which is better than nothing but nominative typing is insufficient IMO) to TypeScript, and it was among the best decisions I’ve made in my professional career.
If I had to directly compare the two I think nominative typing is superior only in the case where you have two identically-shaped (down to the property name) classes that have different semantics. I feel like if you are in this situation you should take a very large step back and re-think your decisions.
I've written an enormous amount of code in CoffeeScript since 2013, but have been migrating it to Typescript during the last year, after it became very clear that CoffeeScript is a dead end. One thing that helps a lot with all the braces (instead of whitespace) is using prettier. My editor is heavily integrated with prettier, so with the touch of a key my code is always formatted in some (almost) canonical way.
There’s some learning around how to type things, but not too hard and very worth it. Need at least one teammate / leader who has used c++/java/c#/etc. extensively on large codebases to mentor others.
Teams of one, es6 is fine, typescript setup is probably overkill, especially for an mvp.
I don’t think writing TypeScript actually takes longer once you’re practiced and it eliminates entire categories of error. YMMV, of course, but in 2019 it’s hard for me not to think of a new JavaScript project as somewhat unserious.
I guess it depends on what you’re doing. Setting up the tooling can sometimes be a bit of a pain in combination with other tools which are not designed with typescript in mind from the get go, such as react_on_rails.
If you’re doing a quick mvp in a weekend, I’m still not sure typescript is right. But it really depends on what other tools you are using and how well they integrate. If the mvp works, ya, definitely switch to typescript ASAP. That’s a fine line and really depends on the use case.
- Write/edit code faster - Read/understand code easier
People say this is subjective, but my experience with other developers has shown otherwise. I once asked a team of developers to "just go along with CoffeeScript for 2 weeks" instead of arguing its syntactic decisions (they were developers who had only used C-style syntax languages in the past like Java/C++/JavaScript). All of them ended up loving CoffeeScript and staying with it for years.
Mind you, TypeScript was a very different language back then. Once it started adding features like disjoint unions and the tooling matured, I made the switch.
When you are not writing critical code where lives or huge amounts of money depend on, then I'd say stick to Coffeescript. And when you find a type related bug in your code, fix it and write a test so the bug cannot occur again. Otherwise, first check out Elm, Dart and Livescript. TS is really the last on my list, I've had my share of pain there.
Are there others?
Edit: Well, I guess the JVM would be considered another?
So the devs were able to iterate faster with CS than with TS?