rescript
ReScript is a robustly typed language that compiles to efficient and human-readable JavaScript.
Top Related Projects
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
Compiler for Elm, a functional language for reliable webapps.
A strongly-typed language that compiles to JavaScript
Simple, fast & type safe code that leverages the JavaScript & OCaml ecosystems
The core OCaml system: compilers, runtime system, base libraries
Adds static typing to JavaScript to improve developer productivity and code quality.
Quick Overview
ReScript is a robustly typed language that compiles to efficient and human-readable JavaScript. It's designed to be familiar to JavaScript developers while offering improved type safety and performance. ReScript is built on OCaml and provides seamless interoperability with existing JavaScript code.
Pros
- Fast compilation and excellent performance
- Strong type system with powerful type inference
- Seamless JavaScript interoperability
- Familiar syntax for JavaScript developers
Cons
- Smaller ecosystem compared to TypeScript or Flow
- Learning curve for developers new to functional programming concepts
- Limited IDE support compared to more mainstream languages
- Potential challenges when integrating with complex JavaScript libraries
Code Examples
- Basic function definition and usage:
let add = (a, b) => a + b
let result = add(5, 3)
Js.log(result) // Outputs: 8
- Pattern matching with variants:
type shape =
| Circle(float)
| Rectangle(float, float)
let area = shape =>
switch shape {
| Circle(radius) => Js.Math.PI *. radius *. radius
| Rectangle(width, height) => width *. height
}
let circleArea = area(Circle(5.0))
Js.log(circleArea) // Outputs: 78.53981633974483
- Working with JavaScript APIs:
// Binding to the JavaScript `console.log` function
@val external consoleLog: 'a => unit = "console.log"
// Using the binding
let message = "Hello from ReScript!"
consoleLog(message)
Getting Started
-
Install ReScript globally:
npm install -g rescript
-
Create a new ReScript project:
mkdir my-rescript-project cd my-rescript-project npm init -y npm install rescript
-
Add a
bsconfig.json
file:{ "name": "my-rescript-project", "sources": { "dir": "src", "subdirs": true }, "package-specs": { "module": "es6", "in-source": true }, "suffix": ".bs.js", "bs-dependencies": [] }
-
Create a
src
directory and add aHello.res
file:Js.log("Hello, ReScript!")
-
Add scripts to
package.json
:"scripts": { "build": "rescript", "start": "rescript build -w" }
-
Build and run your project:
npm run build node src/Hello.bs.js
Competitor Comparisons
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
Pros of TypeScript
- Larger ecosystem and community support
- Gradual adoption possible in existing JavaScript projects
- More extensive tooling and IDE support
Cons of TypeScript
- Slower compilation times, especially for large projects
- Type system can be complex and sometimes confusing
- Runtime overhead due to type erasure
Code Comparison
TypeScript:
interface User {
name: string;
age: number;
}
function greet(user: User): string {
return `Hello, ${user.name}!`;
}
ReScript:
type user = {
name: string,
age: int,
}
let greet = (user) => {
`Hello, ${user.name}!`
}
Key Differences
- TypeScript uses a structural type system, while ReScript uses a nominal type system
- ReScript has a smaller learning curve for OCaml/functional programming developers
- TypeScript offers more flexibility in type annotations and inference
- ReScript provides better performance and smaller bundle sizes
- TypeScript has broader framework and library support
Conclusion
TypeScript is more widely adopted and offers greater flexibility, while ReScript focuses on simplicity, performance, and functional programming paradigms. The choice between them depends on project requirements, team expertise, and development priorities.
Compiler for Elm, a functional language for reliable webapps.
Pros of Elm compiler
- Stronger type system with no runtime exceptions
- Built-in architecture for web applications (The Elm Architecture)
- Smaller learning curve for developers new to functional programming
Cons of Elm compiler
- Less flexibility in interoperating with JavaScript
- Smaller ecosystem compared to ReScript
- More opinionated approach, which may limit certain use cases
Code comparison
Elm:
module Main exposing (main)
import Browser
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)
type Msg = Increment | Decrement
main =
Browser.sandbox { init = 0, update = update, view = view }
ReScript:
module Counter = {
type state = int
type action = Increment | Decrement
let reducer = (state, action) =>
switch action {
| Increment => state + 1
| Decrement => state - 1
}
@react.component
let make = () => {
let (count, dispatch) = React.useReducer(reducer, 0)
// ...
}
}
The code comparison showcases the syntax differences between Elm and ReScript, with Elm using a more declarative approach and ReScript leveraging React components. Both languages emphasize type safety and functional programming concepts, but ReScript offers closer integration with the JavaScript ecosystem.
A strongly-typed language that compiles to JavaScript
Pros of PureScript
- More advanced type system with features like higher-kinded types and type classes
- Stronger emphasis on pure functional programming principles
- Larger ecosystem of libraries and tools
Cons of PureScript
- Steeper learning curve, especially for developers new to functional programming
- Slower compilation times compared to ReScript
- Less seamless JavaScript interoperability
Code Comparison
PureScript:
module Main where
import Effect.Console (log)
main :: Effect Unit
main = log "Hello, PureScript!"
ReScript:
Js.log("Hello, ReScript!")
PureScript's code is more verbose and explicitly typed, reflecting its stronger emphasis on type safety and functional purity. ReScript's syntax is more concise and JavaScript-like, making it easier for developers familiar with JavaScript to adopt.
Both languages compile to JavaScript and offer strong type systems, but they cater to different developer preferences and use cases. PureScript is better suited for large-scale applications requiring advanced type-level programming, while ReScript provides a more approachable path for JavaScript developers looking to adopt a typed functional language.
Simple, fast & type safe code that leverages the JavaScript & OCaml ecosystems
Pros of Reason
- More established ecosystem with longer history and wider adoption
- Closer syntax to OCaml, making it easier for OCaml developers to transition
- Better integration with existing OCaml tooling and libraries
Cons of Reason
- Slower compilation times compared to ReScript
- More complex setup and configuration process
- Less focus on JavaScript/web development specific features
Code Comparison
Reason:
let greeting = (name) => {
"Hello " ++ name ++ "!";
};
Js.log(greeting("World"));
ReScript:
let greeting = name => {
`Hello ${name}!`
}
Console.log(greeting("World"))
The code examples show minor syntax differences, with ReScript offering a more JavaScript-like experience. ReScript uses template strings and a built-in Console module, while Reason uses string concatenation and the Js.log function.
Both Reason and ReScript compile to efficient JavaScript, but ReScript is more focused on the JavaScript ecosystem. Reason maintains closer ties to OCaml, which can be advantageous for certain projects but may introduce complexity for pure web development. ReScript offers a more streamlined experience for JavaScript developers, with faster compilation times and simpler setup, but at the cost of some OCaml compatibility.
The core OCaml system: compilers, runtime system, base libraries
Pros of OCaml
- More mature and established ecosystem with a wider range of libraries
- Supports both functional and object-oriented programming paradigms
- Offers more advanced language features like modules and functors
Cons of OCaml
- Steeper learning curve, especially for developers coming from JavaScript
- Less optimized for web development and JavaScript interoperability
- Syntax can be more verbose and less intuitive for some developers
Code Comparison
OCaml:
let rec factorial n =
if n = 0 then 1
else n * factorial (n - 1)
let result = factorial 5
ReScript:
let rec factorial = n =>
switch n {
| 0 => 1
| _ => n * factorial(n - 1)
}
let result = factorial(5)
Summary
OCaml is a more established and versatile language with a broader range of applications, while ReScript is specifically designed for web development with JavaScript interoperability in mind. OCaml offers more advanced language features but has a steeper learning curve, whereas ReScript provides a more familiar syntax for JavaScript developers. The code comparison shows that ReScript's syntax is generally more concise and JavaScript-like, while OCaml's syntax is more traditional and may require more adjustment for developers coming from other languages.
Adds static typing to JavaScript to improve developer productivity and code quality.
Pros of Flow
- Wider adoption and larger community support
- Seamless integration with existing JavaScript codebases
- More flexible type system, allowing for gradual typing
Cons of Flow
- Slower type-checking performance, especially in large projects
- Less precise type inference in some cases
- Requires a separate compilation step for stripping Flow types
Code Comparison
Flow:
// @flow
function add(a: number, b: number): number {
return a + b;
}
ReScript:
let add = (a: int, b: int): int => a + b
Flow uses JavaScript syntax with type annotations, while ReScript has its own syntax that compiles to JavaScript. ReScript's syntax is more concise and closer to OCaml, offering stronger type inference and faster compilation. However, Flow allows for easier integration with existing JavaScript projects and provides more flexibility in type annotations.
Both tools aim to improve JavaScript development by adding static typing, but they take different approaches. Flow focuses on gradual typing and compatibility with JavaScript, while ReScript offers a more opinionated and performant solution at the cost of a steeper learning curve for developers familiar with JavaScript.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
ReScript
Fast, Simple, Fully Typed JavaScript from the Future.
Introduction · Installation · Try Online · Forum · Contribute
ReScript is a robustly typed language that compiles to efficient and human-readable JavaScript. It comes with a lightning fast compiler toolchain that scales to any codebase size.
- Fast and Simple. ReScript cares about a consistent and fast feedback loop for any codebase size. Refactor code, pull complex changes, or switch to feature branches as you please. No sluggish CI builds, stale caches, wrong type hints, or memory hungry language servers that slow you down.
- A Robust Type System. Every ReScript app is fully typed and provides reliable type information for any given value in your program. We prioritize simpler types over complex types for the sake of clarity and easy debugability. No
any
, no magic types, no surpriseundefined
. - Seamless Integration. Use any library from JavaScript, export ReScript libraries to JavaScript, automatically generate TypeScript types. It's like you've never left the good parts of JavaScript at all.
- Tooling that just works out of the box. A builtin pretty printer, memory friendly VSCode & Vim plugins, a stable type system and compiler that doesn't require lots of extra configuration. ReScript brings all the tools you need to build reliable JavaScript, Node and ReactJS applications.
- Easy to adopt â without any lock-in. ReScript was made with gradual adoption in mind. If you ever want to go back to plain JavaScript, just remove all source files and keep its clean JavaScript output. Tell your coworkers that your project will keep functioning with or without ReScript!
ReScript is used by many companies to ship and maintain mission-critical products and is maintained by the ReScript community.
Contents
ð Getting Started
Follow the Installation Guide to set up a new ReScript project or integrate ReScript into your existing JavaScript project.
For more information on building React apps with ReScript, see the rescript-react documentation.
For React Native apps, see the rescript-react-native website.
ð Documentation
The full documentation for the ReScript language can be found on our website.
The source for the ReScript documentation and website is hosted in a separate repo.
ð Upgrading
See the Upgrading Guide for instructions on upgrading to newer ReScript versions.
ð How to Contribute
Contributing Guide
Read our Contributing Guide to learn about our development process, how to propose bugfixes and improvements, and how to build and test your changes to ReScript.
Code of Conduct
The ReScript community has adopted a Code of Conduct that we expect project participants to adhere to. Please read the full text so that you can understand what actions will and will not be tolerated.
Roadmap
You can learn more about our vision for ReScript in the Roadmap.
Discussions
For discussions on ongoing development, see the Development section of the ReScript forum.
ð License
ReScript is licensed under LGPL version 3, with relaxed rules about creating and distributing combined work. See the LICENSE file for details.
The ReScript parser (subdirectory compiler/syntax
) is licensed under the MIT License.
The ninja
subdirectory contains the vendored ninja build system.
Refer to its copyright and license notices for information about its licensing.
ð Acknowledgments
ReScript was originally created by Hongbo Zhang in 2015.
See CREDITS.md for further acknowledgements and project history.
Top Related Projects
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
Compiler for Elm, a functional language for reliable webapps.
A strongly-typed language that compiles to JavaScript
Simple, fast & type safe code that leverages the JavaScript & OCaml ecosystems
The core OCaml system: compilers, runtime system, base libraries
Adds static typing to JavaScript to improve developer productivity and code quality.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot