DocsPlaygroundBlogCommunityPackages
  • Playground
  • Blog
  • Community
  • Packages
  • X
  • Bluesky
  • GitHub
  • Forum
Language ManualAPISyntax LookupReact
Overview
Stdlib
submodules
  • Array
  • ArrayBuffer
  • AsyncIterator
  • BigInt
  • BigInt64Array
    • Constants
    BigUint64Array
    • Constants
  • Bool
  • Console
  • DataView
  • Date
    • UTC
  • Dict
    • t
      t
    • v
      getUnsafe
    • v
      get
    • v
      set
    • v
      delete
    • v
      make
    • v
      fromArray
    • v
      fromIterator
    • v
      toArray
    • v
      keysToArray
    • v
      size
    • v
      isEmpty
    • v
      valuesToArray
    • v
      assign
    • v
      copy
    • v
      forEach
    • v
      forEachWithKey
    • v
      mapValues
    • v
      has
    • v
      ignore
  • Error
    • URIError
    • TypeError
    • SyntaxError
    • ReferenceError
    • RangeError
    • EvalError
  • Exn
  • Float
    • Constants
    Float32Array
    • Constants
    Float64Array
    • Constants
    Int
    • Ref
    • Bitwise
    • Constants
    Int16Array
    • Constants
    Int32Array
    • Constants
    Int8Array
    • Constants
  • IntervalId
  • Intl
    • Segments
    • Segmenter
    • RelativeTimeFormat
    • PluralRules
    • NumberFormat
      • Grouping
    • Locale
    • ListFormat
    • DateTimeFormat
    • Collator
    • Common
  • Iterator
  • JSON
    • Decode
    • Encode
    • Classify
    JsError
    • URIError
    • TypeError
    • SyntaxError
    • ReferenceError
    • RangeError
    • EvalError
  • JsExn
  • Lazy
  • List
  • Map
  • Math
    • Int
    • Constants
  • Null
  • Nullable
  • Object
  • Option
  • Ordering
  • Pair
  • Promise
  • RegExp
    • Result
  • Result
  • Set
  • String
  • Symbol
  • TimeoutId
  • Type
    • Classify
  • TypedArray
  • Uint16Array
    • Constants
    Uint32Array
    • Constants
    Uint8Array
    • Constants
    Uint8ClampedArray
    • Constants
  • WeakMap
  • WeakSet
  • Docs / API / Stdlib / Dict

    Dict

    A mutable dictionary with string keys.

    Compiles to a regular JavaScript object.

    t

    RESCRIPT
    type t<'a> = dict<'a>

    Type representing a dictionary of value 'a.

    getUnsafe

    RESCRIPT
    let getUnsafe: (dict<'a>, string) => 'a

    getUnsafe(dict, key) Returns the value at the provided key.

    This is unsafe, meaning it will return undefined value if key does not exist in dict.

    Use Dict.getUnsafe only when you are sure the key exists (i.e. when iterating Dict.keys result).

    Examples

    RESCRIPT
    let dict = dict{"key1": "value1", "key2": "value2"} let value = dict->Dict.getUnsafe("key1") Console.log(value) // value1

    get

    RESCRIPT
    let get: (dict<'a>, string) => option<'a>

    Returns the value at the provided key, if it exists. Returns an option.

    Examples

    RESCRIPT
    let dict = dict{"someKey": "someValue"} switch dict->Dict.get("someKey") { | None => Console.log("Nope, didn't have the key.") | Some(value) => Console.log(value) }

    set

    RESCRIPT
    let set: (dict<'a>, string, 'a) => unit

    set(dictionary, key, value) sets the value at the provided key to the provided value.

    Examples

    RESCRIPT
    let dict = Dict.make() dict->Dict.set("someKey", "someValue")

    delete

    RESCRIPT
    let delete: (dict<'a>, string) => unit

    delete(dictionary, key) deletes the value at key, if it exists.

    Examples

    RESCRIPT
    let dict = dict{"someKey": "someValue"} dict->Dict.delete("someKey")

    make

    RESCRIPT
    let make: unit => dict<'a>

    make() creates a new, empty dictionary.

    Examples

    RESCRIPT
    let dict1: dict<int> = Dict.make() // You can annotate the type of the values of your dict yourself if you want let dict2 = Dict.make() // Or you can let ReScript infer it via usage. dict2->Dict.set("someKey", 12)

    fromArray

    RESCRIPT
    let fromArray: array<(string, 'a)> => dict<'a>

    fromArray(entries) creates a new dictionary from the provided array of key/value pairs.

    Examples

    RESCRIPT
    let dict = Dict.fromArray([("key1", "value1"), ("key2", "value2")])

    fromIterator

    RESCRIPT
    let fromIterator: Iterator.t<(string, 'a)> => dict<'a>

    fromIterator(entries) creates a new dictionary from the provided iterator of key/value pairs.

    Examples

    RESCRIPT
    let iterator: Iterator.t<(string, int)> = %raw(` (() => { var map1 = new Map(); map1.set('first', 1); map1.set('second', 2); var iterator1 = map1[Symbol.iterator](); return iterator1; })() `) iterator ->Dict.fromIterator ->Dict.valuesToArray == [1, 2]

    toArray

    RESCRIPT
    let toArray: dict<'a> => array<(string, 'a)>

    toArray(dictionary) returns an array of all the key/value pairs of the dictionary.

    Examples

    RESCRIPT
    let dict = Dict.make() dict->Dict.set("someKey", 1) dict->Dict.set("someKey2", 2) let asArray = dict->Dict.toArray Console.log(asArray) // Logs `[["someKey", 1], ["someKey2", 2]]` to the console

    keysToArray

    RESCRIPT
    let keysToArray: dict<'a> => array<string>

    keysToArray(dictionary) returns an array of all the keys of the dictionary.

    Examples

    RESCRIPT
    let dict = Dict.make() dict->Dict.set("someKey", 1) dict->Dict.set("someKey2", 2) let keys = dict->Dict.keysToArray Console.log(keys) // Logs `["someKey", "someKey2"]` to the console

    size

    RESCRIPT
    let size: dict<'a> => int

    size(dictionary) returns the number of key/value pairs in the dictionary.

    Examples

    RESCRIPT
    let dict = Dict.make() dict->Dict.size->assertEqual(0) dict->Dict.set("someKey", 1) dict->Dict.set("someKey2", 2) dict->Dict.size->assertEqual(2) // After deleting a key dict->Dict.delete("someKey") dict->Dict.size->assertEqual(1)

    isEmpty

    RESCRIPT
    let isEmpty: dict<'a> => bool

    isEmpty(dictionary) returns true if the dictionary is empty (has no key/value pairs), false otherwise.

    Examples

    RESCRIPT
    let emptyDict = Dict.make() emptyDict->Dict.isEmpty->assertEqual(true) let dict = Dict.make() dict->Dict.set("someKey", 1) dict->Dict.isEmpty->assertEqual(false) // After clearing all keys dict->Dict.delete("someKey") dict->Dict.isEmpty->assertEqual(true)

    valuesToArray

    RESCRIPT
    let valuesToArray: dict<'a> => array<'a>

    valuesToArray(dictionary) returns an array of all the values of the dictionary.

    Examples

    RESCRIPT
    let dict = Dict.make() dict->Dict.set("someKey", 1) dict->Dict.set("someKey2", 2) let values = dict->Dict.valuesToArray Console.log(values) // Logs `[1, 2]` to the console

    assign

    RESCRIPT
    let assign: (dict<'a>, dict<'a>) => dict<'a>

    assign(dictionary1, dictionary2) shallowly merges dictionary2 into dictionary1, and returns dictionary1.

    Beware this will mutate dictionary1. If you're looking for a way to copy a dictionary, check out Dict.copy.

    Examples

    RESCRIPT
    let dict1 = Dict.make() dict1->Dict.set("firstKey", 1) Console.log(dict1->Dict.keysToArray) // Logs `["firstKey"]` let dict2 = Dict.make() dict2->Dict.set("someKey", 2) dict2->Dict.set("someKey2", 3) let dict1 = dict1->Dict.assign(dict2) Console.log(dict1->Dict.keysToArray) // Logs `["firstKey", "someKey", "someKey2"]`

    copy

    RESCRIPT
    let copy: dict<'a> => dict<'a>

    copy(dictionary) shallowly copies the provided dictionary to a new dictionary.

    Examples

    RESCRIPT
    let dict = dict{"key1": "value1", "key2": "value2"} let dict2 = dict->Dict.copy // Both log `["key1", "key2"]` here. Console.log2(dict->Dict.keysToArray, dict2->Dict.keysToArray)

    forEach

    RESCRIPT
    let forEach: (dict<'a>, 'a => unit) => unit

    forEach(dictionary, f) iterates through all values of the dict.

    Please note that this is without the keys, just the values. If you need the key as well, use Dict.forEachWithKey.

    Examples

    RESCRIPT
    let dict = dict{"key1": "value1", "key2": "value2"} dict->Dict.forEach(value => { Console.log(value) })

    forEachWithKey

    RESCRIPT
    let forEachWithKey: (dict<'a>, ('a, string) => unit) => unit

    forEachWithKey(dictionary, f) iterates through all values of the dict, including the key for each value.

    Examples

    RESCRIPT
    let dict = dict{"key1": "value1", "key2": "value2"} dict->Dict.forEachWithKey((value, key) => { Console.log2(value, key) })

    mapValues

    RESCRIPT
    let mapValues: (dict<'a>, 'a => 'b) => dict<'b>

    mapValues(dictionary, f) returns a new dictionary with the same keys, and f applied to each value in the original dictionary.

    Examples

    RESCRIPT
    let dict = dict{"key1": 1, "key2": 2} dict->Dict.mapValues(v => v + 10)->Dict.toArray // [("key1", 11), ("key2", 12)] dict->Dict.mapValues(v => Int.toString(v))->Dict.toArray // [("key1", "1"), ("key2", "2")]

    has

    RESCRIPT
    let has: (dict<'a>, string) => bool

    has(dictionary, "key") returns true if the "key" is present in the dictionary.

    Be aware that it uses the JavaScript in operator under the hood.

    Examples

    RESCRIPT
    let dict = dict{"key1": Some(1), "key2": None} dict->Dict.has("key1") == true dict->Dict.has("key2") == true dict->Dict.has("key3") == false dict->Dict.has("toString") == true

    ignore

    RESCRIPT
    let ignore: dict<'a> => unit

    ignore(dict) ignores the provided dict and returns unit.

    This helper is useful when you want to discard a value (for example, the result of an operation with side effects) without having to store or process it further.

    Types and values
    • t
      t
    • v
      getUnsafe
    • v
      get
    • v
      set
    • v
      delete
    • v
      make
    • v
      fromArray
    • v
      fromIterator
    • v
      toArray
    • v
      keysToArray
    • v
      size
    • v
      isEmpty
    • v
      valuesToArray
    • v
      assign
    • v
      copy
    • v
      forEach
    • v
      forEachWithKey
    • v
      mapValues
    • v
      has
    • v
      ignore

    © 2025 The ReScript Project

    About
    • Community
    • ReScript Association
    Find us on