ReScript Basics¶
This page helps you get a ReScript project up and running if you’re new to ReScript.
Tip
For comprehensive ReScript documentation, visit the official ReScript docs.
Setting Up a New Project¶
Option 1: Using the Project Wizard¶
File → New → Project
Select ReScript from the generator list
Fill in the project name and location
Click Create
The wizard generates a basic project structure with rescript.json and source files.
Option 2: Manual Setup¶
Create a new directory for your project
Initialize with npm:
mkdir my-rescript-project
cd my-rescript-project
npm init -y
Install ReScript:
npm install rescript @rescript/core
Install the Language Server:
npm install @rescript/language-server
Create
rescript.json:
{
"name": "my-rescript-project",
"sources": [
{ "dir": "src", "subdirs": true }
],
"package-specs": [
{ "module": "esmodule", "in-source": true }
],
"bs-dependencies": ["@rescript/core"]
}
Create
src/directory and your first.resfile:
mkdir src
// src/Main.res
let greeting = "Hello from ReScript!"
Console.log(greeting)
Build:
npx rescript build
Project Structure¶
A typical ReScript project looks like:
my-project/
├── rescript.json # ReScript configuration
├── package.json # npm dependencies
├── node_modules/
│ ├── rescript/ # ReScript compiler
│ └── @rescript/
│ ├── core/ # Standard library
│ └── language-server/ # LSP server
├── src/
│ ├── Main.res # Source files
│ └── Utils.res
└── lib/ # Compiled output (auto-generated)
Key Files¶
rescript.json¶
The main configuration file for your ReScript project. The plugin provides JSON Schema validation and auto-completion for this file.
Key fields:
name— Package namesources— Source directories to compilebs-dependencies— ReScript package dependenciespackage-specs— Output format (commonjs or esmodule)
.res Files¶
ReScript source files. Each .res file defines a module with the same name as the file.
.resi Files¶
ReScript interface files. They define the public API of the corresponding .res module. Use Alt+O to switch between .res and .resi files.
Basic Syntax Quick Reference¶
// Variables
let name = "ReScript"
let age = 25
let isReady = true
// Functions
let add = (a, b) => a + b
let greet = (name) => `Hello, ${name}!`
// Types
type color = Red | Green | Blue
type user = {name: string, age: int}
// Pattern matching
let describe = (color) =>
switch color {
| Red => "red"
| Green => "green"
| Blue => "blue"
}
// Modules
module Math = {
let pi = 3.14159
let square = (x) => x *. x
}
// Pipe operator
let result = [1, 2, 3]->Array.map(x => x * 2)->Array.filter(x => x > 2)