Why Learn ReScript?
JavaScript is vital to the modern web ecosystem. Its used in the front end to implement websites and other user interfaces, and used in the back end to implement servers for websites and APIs.
Part of JavaScripts ubiquity is due to its ease of use. JavaScript is dynamic and flexible, making it easy for people to pick up. However, this strength becomes a weakness when working on large web applications with multiple developers the only way to know that JavaScript code works correctly is to actually run it, and its relatively easy to make mistakes when programming in JavaScript.
What if there was a way to detect bugs in JavaScript before running the code, or prevent many classes of bugs altogether? What if there was a language that was concise and elegant that made it easy for programmers to write complex web applications and hard for programmers to make mistakes?
Enter ReScript.
ReScript is a language designed for writing web applications. It brings a lot to the table: static typechecking, a strong type system, and powerful language features that will change the way you program.
Heres a glimpse of some of the features that make ReScript a great language:
Static typechecking Catch bugs in your code without having to run it: undefined values, missing cases, incorrect types, and more.
Sound type system ReScript programs that pass typechecking cannot have runtime type errors.
Type inference ReScript automatically infers types based on how variables are used, allowing you to enjoy the benefits of type safety without having to annotate every variable and function.
Immutability Peace of mind while you program with variables and data structures that cannot be unexpectedly modified under your nose.
Algebraic data types and pattern matching Cleanly define and elegantly manipulate complex data.
First-class bindings for React Write React elements and JSX directly inside ReScript files.
There are a number of other languages and tools that offer static typechecking for web applications, but ReScript has several key advantages over its competitors. As an example, lets look at the benefits ReScript has compared with another popular JavaScript alternative, TypeScript:
ReScript is safer Unlike ReScripts battle-tested and sound type system, TypeScripts type system is unsound, so it is still possible to have runtime type errors in a valid TypeScript program.
ReScript is faster ReScripts compiler is much faster than TypeScripts compiler, allowing for a smoother development experience when working in large code bases.
ReScript is more concise ReScripts excellent type inference means that programmers do not have to write as many type annotations in ReScript programs compared to TypeScript programs.
Although ReScript is a relative newcomer to the web ecosystem, its actually based on technology that has been battle-tested for years before ReScript even existed. ReScript itself has proven successful as well. Most notably, Facebook used it to build the web interface for Messenger a product used by hundreds of millions of people with a code base containing thousands of files.
History of ReScript
The lineage of ReScript can ultimately be traced back to the ML family of languages originating from the 1960s. In particular, ReScript is directly based on OCaml, a general-purpose programming language that was developed in the 1980s and used today for systems programming in academia and industry.
In 2015, Jordan Walke, the creator of the React web framework, developed a toolchain and alternative syntax for OCaml called Reason.
Reason was designed to bridge the gap between the web and OCaml ecosystems it could be compiled into both native machine code and JavaScript, allowing web developers to take advantage of OCamls features. Static typechecking and OCamls sound type system eliminated many common bugs in JavaScript code, and OCamls immutability and functional style was a great fit for React.
Reason was compiled to JavaScript using a compiler called BuckleScript, which was developed at Bloomberg around the same time Reason was being created at Facebook.
Around 2020, the BuckleScript project created a new language based on Reason that could only be compiled to JavaScript using the BuckleScript compiler, and so ReScript was born.
ReScript has the following key differences from its predecessors:
ReScript has different syntax and features. While it looks and feels more like JavaScript, ReScript is still based on the battle-tested compiler and type system as Reason and OCaml, so it has the same type safety benefits as its predecessors.
ReScript can only be compiled to JavaScript. By dropping support for native compilation, ReScript has a simpler toolchain and standard library, along with a feature set better suited for web development. This makes ReScript easier for newcomers to learn and allows for smoother integration with other web technologies.
ReScript and the Web Ecosystem
Like some other statically typed languages in the web ecosystem, ReScript code is transpiled to JavaScript. This means that ReScript code doesnt directly run in the browser or on the server. Instead, the ReScript compiler checks that the code is valid and generates JavaScript files, which can then be imported and used like any handwritten JavaScript file.