The font in the picture is called ‘Operator Mono’.
At the start of this year, Reddit CEO Steve Decided to redesign Reddit (finally). Then they start thinking about all kinds of questions, but the first one is “What language?”
Of course, the language you choose will eventually be compiled into JavaScript, but choosing the right language during development can make a huge difference in productivity. Here are some of our options:
- Bucklescript
- ClojureScript
- Coffeescript
- Elm
- Elixirscript
- Javascript 2016 and beyond
- Javascript + annotations
- Nim
- PureScript
- Reason
- TypeScript
Each language has its pros and cons, so we’ve identified a few of our requirements:
- Support strong typing.
- Have a good kit.
- There are already success stories.
- Our engineers can pick it up very quickly.
- Able to work on both client and server.
- Good class libraries.
After considering these requirements, our best options seem to be Typescript or JavaScript + Flow. Before we choose, let’s see what the differences are.
Compilation vs Annotation
One major difference between Typescript and Flow is that Typescript is a strongly typed version of JavaScript, whereas Flow is through a set of annotations that can be added to JavaScript and then checked for correctness by tools. For example, enum is written:
Typescript
enum VoteDirection {
upvoted = 1,
notvoted = 0,
downvoted = -1,
};
const voteState: VoteDirection = VoteDirection.upvoted;
Copy the code
Flow
const voteDirections = {
upvoted: 1,
notvoted: 0,
downvoted: -1,
};
type VoteDirection = $Keys<typeof voteDirections>;
const voteState: VoteDirection = voteDirections.upvoted;
Copy the code
In Reddit we use Babel for transcoding, there are some optimizations we want to keep, and TypeScript’s own compilation process can cause us problems. So we need to adapt our build steps to integrate TypeScript so that we don’t break much of what we already have.
By contrast, Flow type annotations are automatically removed by Babel, so if we use Flow, our build steps will be kept as simple as possible.
correctness
Flow is generally thought to do a better job of type correctness. Flow doesn’t allow nullable by default, whereas TypeScript didn’t support nullable until 2.x. And Flow is better at inferring types, whereas TypeScript tends to fall back to the ‘any’ type.
Here’s an example of an array:
Flow
class Animal {}
class Bird extends Animal {}
const foo: Array<Bird> = [];
foo.push(new Animal());
/*
foo.push(new A);
^ A. This type is incompatible with
const foo: Array<B> = [];
^ B
*/
Copy the code
Typescript
class Animal {}
class Bird extends Animal {}
const foo: Array<Bird> = [];
foo.push(new Animal()); // ok in typescript
Copy the code
There are many more examples online, but the general consensus is that Flow does a better job of type checking than TypeScript.
The ecological system
Until now, Flow has allowed us to have a simpler build process and better type checking, so why TypeScript?
One of the great things about TypeScript is its ecosystem, and the support library for TypeScript is awesome. All the libraries we use are indexed DefinitelyTyped and are of very good quality.
It also supports popular editors such as VSCode, Atom, and Sublime Text. In addition, TypeScript supports parsing JSDoc, which is really useful.
There are already several mature projects using TypeScript, including VSCode, Rxjs, and Angula. Therefore, we believe that its functions can meet our product needs. In addition, one of our concerns with Flow is that Flow is built to address the specific needs of Facebook, so its future development will be more constrained by Facebook’s business needs.
conclusion
We chose Typescript because we were confident that our engineers (who have doubled in the last year) could pick it up quickly. But more importantly, it’s strongly typed, so our code has fewer type-related bugs, it’s easier to build large applications, and it has a rich ecosystem. All in all, we are happy with our choice.
The original:
Why We Chose Typescript
Daily Extended reading:
Think about how to classify logs from another Angle – Zhihu Column
Welcome to Zhihu’s Aurora Daily, which provides Makers with three quality English articles a day.