I already talked about the 7 reasons not to use TypeScript earlier, but failed to provide an alternative. In this article I will introduce you to ReasonML, a better alternative to TypeScript.
ReasonML is not exactly a new language or a language at all. It is based on OCaml, which, in turn, is based on the ML language. These, along with a few other languages (such as Rust, Scala or Elm) are considered members of the ML family. Their main traits are focus on functional programming, strong typing, and immutability.
ResonML is basically a toolchain and syntax improvements for OCaml, to make it well suited for web development. As you probably guessed, it compiles to JavaScript and lets you interact with existing JS packages with no hassle. However, it differs from TypeScript in a few key concepts.
Firstly, ReasonML guarantees type safety at compile time and runtime. It is a little something that is inherited from OCaml and I cannot stress just how important this is. Moreover, while TypeScript will warn you about type mismatches but still compile, ReasonML will not compile at all. It may be a bit draconian but I consider the resulting peace of mind worth it.
Secondly, ReasonML is based on OCaml, which is widely spread and was around for a few decades. It implements functional programming the way it was designed in the first place and is natively immutable. TypeScript, on the other hand, tries to be a one-size-fits-all language, implementing both functional and object-oriented paradigms. As a result, it has a poor implementation of both.
Thirdly, ReasonML just does types better than TypeScript. It offers all the same features that people love in TypeScript, such as type inference, interfaces (called records in ReasonML) and linting. But it does all of these better, thanks to its functional legacy. For example, consider this example in TypeScript:
You can see that from this code TypeScript assumes that the types of x
and y
should be any
, as well as the return value. The same code in ReasonML:
ReasonML deduces that types of a
, b
and the return value must be integers and will warn you if you try to pass in a string.
ReasonML also has a huge community and is backed by Facebook, so there are no reasons to worry about support and lack of material. And, since you can use any JS package out-of-box, there is nothing you can’t do with ReasonML.
As a bonus, ReasonML can also compile to OCaml directly, which, in turn, can compile to native code. Yes, you can develop truly native apps as well as web apps using the same codebase!
Examples
Hopefully, I have convinced you to at least give ReasonML a try. Here are some examples of common ReasonML syntax:
Thank you for reading! Let me know what do you think about ResonML and what tutorial on ReasonML would you like me to write. Also, feel free to check out my other articles:
- 10 JavaScript interview questions for 2020
- To infinity and beyond with JavaScript Proxy API
- No, you do not need Windows to develop with JavaScript
Can this be added to an existing Sapper.js project for example?
ReasonML compiles directly onto JavaScript, so it can be integrated into any JS project! Let me know how it goes if you do decide to adopt it!