You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: "Introduction to the ReScript programming language"
4
4
canonical: "/docs/manual/v12.0.0/introduction"
5
5
---
6
6
7
7
# ReScript
8
8
9
-
Ever wanted a language like JavaScript, but without the warts, with a great type system, and with a lean build toolchain that doesn't waste your time?
9
+
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.
10
10
11
-
ReScript looks like JS, acts like JS, and compiles to the highest quality of clean, readable and performant JS, directly runnable in browsers and Node.
11
+
## Part of the JavaScript ecosystem
12
+
ReScript looks like JS, acts like JS, and compiles to the highest quality of clean, readable and performant JS, directly runnable in browsers and Node. This means you can pick up ReScript and access the vast JavaScript ecosystem and tooling as if you've known ReScript for a long time!
12
13
13
-
**This means you can pick up ReScript and access the vast JavaScript ecosystem and tooling as if you've known ReScript for a long time!**
14
+
You don't need to learn new package managers, bundlers, frameworks, or testing libraries. All of the knowledge you have about doing web development with JavaScript can be applied to building applications with Rescript.
14
15
15
-
**ReScript is the language for folks who don't necessarily love JavaScript, but who still acknowledge its importance**.
16
-
17
-
## Difference vs TypeScript
18
-
19
-
We respect TypeScript very much and think that it's a positive force in the JavaScript ecosystem. ReScript shares some of the same goals as TypeScript, but is different enough regarding some important nuances:
20
-
21
-
- TypeScript's (admittedly noble) goal is to cover the entire JavaScript feature set and more. **ReScript covers only a curated subset of JavaScript**. For example, we emphasize plain data + functions over classes, clean [pattern matching](pattern-matching-destructuring.md) over fragile `if`s and virtual dispatches, [proper data modeling](variant.md) over string abuse, etc. JavaScript supersets will only grow larger over time; ReScript doesn't. \*
22
-
23
-
- Consequently, TypeScript's type system is necessarily complex, pitfalls-ridden, potentially requires tweaking, sometimes slow, and requires quite a bit of noisy annotations that often feel like manual bookkeeping rather than clear documentation. In contrast, ReScript's type system:
16
+
ReScript code can be [imported into JavaScript code](/docs/manual/latest/import-from-export-to-js#export-to-javascript), can [generate types for TypeScript](/docs/manual/latest/typescript-integration), and ReScript can [import code written in JavaScript or TypeScript](/docs/manual/latest/import-from-export-to-js#import-from-javascript).
24
17
18
+
## Type System
25
19
- Is deliberately curated to be a simple subset most folks will have an easier time to use.
26
20
- Has **no** pitfalls, aka the type system is "sound" (the types will always be correct). E.g. If a type isn't marked as nullable, its value will never lie and let through some `undefined` value silently. **ReScript code has no null/undefined errors**.
27
21
- Is the same for everyone. No knobs, no bikeshedding opportunity.
28
22
- Runs extremely fast precisely thanks to its simplicity and curation. It's one of the fastest compiler & build system toolchains for JavaScript development.
29
23
-**Doesn't need type annotations**. Annotate as much or as little as you'd like. The types are inferred by the language (and, again, are guaranteed correct).
30
24
31
-
- Migrating to TypeScript is done "breadth-first," whereas migrating to ReScript is done "depth-first." You can convert your codebase to TypeScript by "turning it on" for all files and annotate here and there; but how much type safety did you gain? How do you measure it? Type errors can still slip in and out of the converted pieces. For ReScript, our interop features draw clear boundaries: there's pure ReScript code, and there's JS interop code. Every piece of converted ReScript code is 100% clean. You'd convert file by file and each conversion increases your safety monotonically.
32
-
33
-
\* When you absolutely need to write or interoperate with free-for-all JavaScript, we expose enough escape hatches for you.
34
-
35
-
## Other Highlights
36
-
37
-
Aside from the aforementioned simple, robust and fast type system, ReScript presents a few more advantages.
38
-
39
-
### Faster than JavaScript
25
+
## Compiler
26
+
### Compiles to Optimized JavaScript
40
27
41
28
JavaScript's been aggressively optimized by talented engineers over a long span. Unfortunately, even for seasoned JS devs, it can be hard to know how to properly leverage JS's performance. ReScript's type system and compiler naturally guides you toward writing code that's very often performant by default, with good leverage of various Just-In-Time optimizations (hidden classes, inline caching, avoiding deopts, etc).
42
29
43
30
A widespread adage to write fast JavaScript code is to write as if there's a type system (in order to trigger JS engines' good optimization heuristics); ReScript gives you a real one and generates code that's friendly to optimizations by default.
44
31
45
-
### High Quality Dead Code Elimination
46
-
47
-
The JavaScript ecosystem is very reliant on dependencies. Shipping the final product inevitably drags in a huge amount of code, lots of which the project doesn't actually use. These regions of dead code impact loading, parsing and interpretation speed. ReScript provides powerful dead code elimination at all levels:
48
-
49
-
- Function- and module-level code elimination is facilitated by the well-engineered type system and purity analysis.
50
-
- At the global level, ReScript generates code that is naturally friendly to dead code elimination done by bundling tools such as [Rollup](https://github.com/rollup/rollup) and [Closure Compiler](https://developers.google.com/closure/compiler/), after its own sophisticated elimination pass.
51
-
- The same applies for ReScript's own tiny runtime (which is written in ReScript itself).
52
-
53
32
### Tiny JS Output
54
33
55
34
A `Hello world` ReScript program generates **20 bytes** of JS code. Additionally, the standard library pieces you require in are only included when needed.
@@ -58,14 +37,7 @@ A `Hello world` ReScript program generates **20 bytes** of JS code. Additionally
58
37
59
38
ReScript's build time is **one or two orders of magnitude** faster than alternatives. In its watcher mode, the build system usually finishes before you switch screen from the editor to the terminal tab (two digits of milliseconds). A fast iteration cycle reduces the need of keeping one's mental state around longer; this in turn allows one to stay in the flow longer and more often.
60
39
61
-
### Readable Output & Great Interop
62
-
63
-
Unreadable JavaScript code generated from other compiled-to-js languages makes it so that it could be, practically speaking:
64
-
65
-
- Hard to debug (cryptic stack trace, mangled variable names)
66
-
- Hard to learn from (non-straightforward mapping of concepts from one language to another)
67
-
- Hard to profile for performance (unclear what runtime performance cost there is)
68
-
- Hard to integrate with existing hand-written JS code
40
+
### Readable Output
69
41
70
42
ReScript's JS output is very readable. This is especially important while learning, where users might want to understand how the code's compiled, and to audit for bugs.
71
43
@@ -75,6 +47,25 @@ This characteristic, combined with a fully-featured JS interop system, allows Re
75
47
76
48
ReScript maps one source file to one JavaScript output file. This eases the integration of existing tools such as bundlers and test runners. You can even start writing a single file without much change to your build setup. Each file's code structure is approximately preserved, too.
77
49
50
+
### High Quality Dead Code Elimination
51
+
52
+
The JavaScript ecosystem is very reliant on dependencies. Shipping the final product inevitably drags in a huge amount of code, lots of which the project doesn't actually use. These regions of dead code impact loading, parsing and interpretation speed. ReScript provides powerful dead code elimination at all levels:
53
+
54
+
- Function- and module-level code elimination is facilitated by the well-engineered type system and purity analysis.
55
+
- At the global level, ReScript generates code that is naturally friendly to dead code elimination done by bundling tools such as [Rollup](https://github.com/rollup/rollup) and [Closure Compiler](https://developers.google.com/closure/compiler/), after its own sophisticated elimination pass.
56
+
- The same applies for ReScript's own tiny runtime (which is written in ReScript itself).
57
+
58
+
## Difference vs TypeScript
59
+
60
+
We respect TypeScript very much and think that it's a positive force in the JavaScript ecosystem. ReScript shares some of the same goals as TypeScript, but is different enough regarding some important nuances:
61
+
62
+
- TypeScript's (admittedly noble) goal is to cover the entire JavaScript feature set and more. **ReScript covers only a curated subset of JavaScript**. For example, we emphasize plain data + functions over classes, clean [pattern matching](pattern-matching-destructuring.md) over fragile `if`s and virtual dispatches, [proper data modeling](variant.md) over string abuse, etc. JavaScript supersets will only grow larger over time; ReScript doesn't. \*
63
+
64
+
- Consequently, TypeScript's type system is necessarily complex, pitfalls-ridden, potentially requires tweaking, sometimes slow, and requires quite a bit of noisy annotations that often feel like manual bookkeeping rather than clear documentation.
65
+
- Migrating to TypeScript is done "breadth-first," whereas migrating to ReScript is done "depth-first." You can convert your codebase to TypeScript by "turning it on" for all files and annotate here and there; but how much type safety did you gain? How do you measure it? Type errors can still slip in and out of the converted pieces. For ReScript, our interop features draw clear boundaries: there's pure ReScript code, and there's JS interop code. Every piece of converted ReScript code is 100% clean. You'd convert file by file and each conversion increases your safety monotonically.
66
+
67
+
\* When you absolutely need to write or interoperate with free-for-all JavaScript, we expose enough escape hatches for you.
68
+
78
69
## Conclusion
79
70
80
71
We hope the above gave you enough of an idea of ReScript and its differentiators. Feel free to [try it online](/try) to get a feel!
0 commit comments