Calypso could migrate to TypeScript #893

Closed
pdfernhout opened this Issue Nov 27, 2015 · 2 comments

Projects

None yet

3 participants

@pdfernhout

Calypso could migrate to TypeScript to help with maintainability and learnability of the codebase.

=== More details

JavaScript can be a very difficult language to work in for various reasons including due to to some design flaws in it -- even if people can work around those flaws and do good work in JavaScript anyway. The bigger a JavaScript project gets, the more difficulties can pile up though. At some point, quantitative changes in the codebase of ever more lines of JavaScript produce qualitative changes, and JavaScript code can move from "flexible" to "flabby". :-) Good programmer discipline and a good lint tool can help with that, but only so far. As a codebase grows and as time goes on, significant refactoring becomes more and more likely. Here is an example of a small refactoring proposal from yesterday by @nb. I would argue that if Calypso was in TypeScript, such refactoring would be easier to do with higher confidence the result would be correct. You still need tests and coding standards and such of course; TypeScript is no panacea -- just an improvement.

To support easier refactoring (including changing GUI toolkits), I converted a medium-sized-compared-to-Calypso project I had worked on (a WordPress plugin called NarraFirma) from JavaScript to TypeScript once the project exceeded about 100 JavaScript files and refactoring became significantly harder. It is now hard to for me to imagine going back to plain JavaScript for the client side of that project, and I wish I had made the move much sooner given the scale of that part of the codebase. By contrast, the server side of that application is only a handful of JavaScript files, so migrating that to TypeScript has not been a priority.

A TypesScript codebase is also generally easier for people to learn than one in plain JavaScript because autocomplete generally works better in TypeScript IDEs than JavaScript ones. That helps when new people need to maintain existing code -- even people who don't know either JavaScript or TypeScript that well.

There are of course other languages one could port JavaScript to that still compiled or transpile to JavaScript. However, TypeScript seems like the lowest risk move for large existing JavaScript codebases. The Angular 2 project picked TypeScript for future work, as has the Dojo 2.0 project. A big advantage of TypeScript is that you can still always write regular JavaScript if you absolutely want to or need to. While TypeScript is a project out of Microsoft, there is likely minimal legal and technical risk of using it in the sense that it is always possible to generate readable JavaScript from the TypeScript and go back to coding in plain JavaScript. Even though very few people who have used TypeScript would seriously want to consider going back to plain JavaScript for a big project. :-)

There are people who might strongly disagree with the value of TypeScript, as in this essay "TypeScript isn't the answer", so the issue is not without controversy. Comments there tend to support TypeScript though. As David Kapetyan says (and reflects my own experience with TypeScript):

"You should use typescript on a non-trivial project before saying it doesn't fix anything. It actually fixes a lot. Gradually typed languages, which typescript falls into, are a great compromise between static and dynamic types. You get rapid [prototyping] when you need it and you get static guarantees when the design is fleshed out. The kind of feedback that tsc provides when you start adding types is invaluable and adding types to legacy javascript libraries is much easier than you think."

Ideally, the initial conversion process should not take that long (mostly renaming file extensions, sometimes changing imports, adding some basic stub typing for libraries, setting TypeScript compiler options, configuring your IDE, and adding another step of calling the TypeScript compiler to the build process). That whole process for Calypso should probably not take much more than a week or so if a few people did it together who were already familiar with the Calypso codebase. Then further improvements to Calypso could be done incrementally. Helping with that might make a good Automattic trial project for someone. :-)

There are downsides to working in TypeScript in that debugging can become a bit harder -- even with source maps (which also slow the build process if you generate them). However, overall, that tradeoff of slightly harder debugging for much easier refactoring and also catching many more errors before they make it to runtime and even production seems worth it to many people.

TypeScript 1.6 would be required to get JSX support for the current Calypso codebase. This issue is a spinoff from issue #650, although migrating to TypeScript would make any sort of major refactoring of Calypso easier. Moving Calypso to TypeScript is just a good thing to do for multiple reasons.

@apeatling
Member

Thanks for the thought, but that's not on our radar at this time.

@apeatling apeatling closed this Nov 27, 2015
@pdfernhout

@apeatling Thanks for the reply. As something to think about down the road, what sort of metrics or other evidence about developer happiness could put something like TypeScript or something similar on the radar?

Maybe trending twitters? :-) [Although this one is from someone at Microsoft...]
https://twitter.com/fearthecowboy/status/635868525609877505
"Happiness is coding in Typescript all day long."

Likewise, how could metrics about code quality, user experience bug reports, or time to do refactoring be linked to a consideration of TypeScript (or whatever)? I'm not asking that to get an answer now. I'm just suggesting it as a question to think about in the future, given Automattic (sensibly) likes using metrics for decision making when possible (even given, as Scott Berkun pointed out, there is no easy-to-use metric for deciding what metrics to use or how to weigh competing metrics).

It took me many months to decide to make the leap from JavaScript to TypeScript, although now I wish I had done it sooner. That was only after a long time considering a bewildering array of options that either compile or transpile to JavaScript (many of which are also quite nice).

No doubt Automattic will continue to reflect on emerging technologies and their various benefits and risks as time goes by. So, I hope you'll revisit the decision in a year or so, or perhaps before then even try TypeScript in some small part of the Calypso codebase or on another project and see how it goes.

In any case, Calypso is a great move forward for WordPress, whatever it is coded in. :-) Thanks for providing the leadership to make that happen.

@apeatling apeatling locked and limited conversation to collaborators Dec 1, 2015
@apeatling apeatling unlocked this conversation Dec 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment