DocsPlaygroundBlogCommunity
  • Playground
  • Blog
  • Community
  • X
  • Bluesky
  • GitHub
  • Forum
Language ManualAPISyntax LookupReact
Language Manual
Overview
  • Introduction
  • Installation
  • Editor
  • LLMs
Guides
  • Converting from JS
  • Dead Code Analysis in ReScript
  • Project Structure
JavaScript Interop
  • Interop Cheatsheet
  • Embed Raw JavaScript
  • Shared Data Types
  • External (Bind to Any JS Library)
  • Bind to JS Object
  • Bind to JS Function
  • Import from / Export to JS
  • Bind to Global JS Values
  • JSON
  • Inlining Constants
  • Use Illegal Identifier Names
  • Generate Converters & Helpers
  • Browser Support & Polyfills
  • Libraries & Publishing
  • TypeScript
Language Features
  • Overview
  • Let Binding
  • Type
  • Primitive Types
  • Tuple
  • Record
  • Object
  • Dictionary
    • Create
    • Access
    • Pattern matching
    • Updating and setting values
    • Advanced example: Pattern matching on JSON
  • Variant
  • Polymorphic Variant
  • Null, Undefined and Option
  • Array & List
  • Function
  • If-Else & Loops
  • Pipe
  • Pattern Matching / Destructuring
  • Mutation
  • JSX
  • Exception
  • Lazy Value
  • Promises
  • Async / Await
  • Tagged templates
  • Module
  • Import & Export
  • Attribute (Decorator)
  • Reserved Keywords
  • Equality and Comparison
Build System
  • Overview
  • Configuration
  • Configuration Schema
  • External Stdlib
  • Pinned Dependencies
  • Interop with JS Build Systems
  • Performance
  • Warning Numbers
Advanced Features
  • Extensible Variant
  • Scoped Polymorphic Types
Docs / Language Manual / Dictionary
Edit

You are currently looking at the v12 docs, which are still a work in progress. If you miss anything, you may find it in the older v11 docs here.

Dictionary

ReScript has first class support for dictionaries. Dictionaries are mutable objects with string keys, where all values must have the same type. Dicts compile to regular JavaScript objects at runtime.

Create

You can create a new dictionary in a few different ways, depending on your use case.

ReScriptJS Output
// Using the first class dict syntax
let d = dict{"A": 5, "B": 6}

// Programatically via the standard library
let d2 = Dict.fromArray([("A", 5), ("B", 6)])

A few things to note here:

  • Using the first class dict{} syntax compiles cleanly to a JavaScript object directly

  • Using Dict.fromArray is useful when you need to create a dictionary programatically

Access

You can access values from a Dictionary either via the the standard library Dict module functions, or using pattern matching.

ReScriptJS Output
let d = dict{"A": 5, "B": 6, "C": 7}

// Using `Dict.get`
let a = d->Dict.get("A")

// Switching on the full dict
let b = switch d {
| dict{"B": b} => Some(b)
| _ => None
}

// Destructuring
let dict{"C": ?c} = d

In the Destructuring example, we're using the ? optional pattern match syntax to pull out the C key value as an optional, regardless of if the dict has it or not.

Pattern matching

Dictionaries have first class support for pattern matching. Read more in the dedicated guide on pattern matching and destructring in ReScript.

Updating and setting values

You can set and update new values on your dictionary using the Dict.set function. All updates are mutable.

ReScriptJS Output
let d = dict{"A": 5, "B": 6}

d->Dict.set("C", 7)

Advanced example: Pattern matching on JSON

JSON objects are represented as dictionaries (dict<JSON.t>). You can leverage that fact to decode JSON in a nice way, using only language features:

ReScriptJS Output
type user = {
  name: string,
  email: string,
}

/** Decode JSON to a `user`. */
let decodeUser = (json: JSON.t) => {
  switch json {
  | Object(dict{"name": JSON.String(name), "email": JSON.String(email)}) =>
    Some({name, email})
  | _ => None
  }
}

ObjectVariant

© 2025 The ReScript Project

Software and assets distribution powered by KeyCDN.

About
  • Community
  • ReScript Association
Find us on