Hyperlinkv0.8.0-beta.28

Record

Record.replaceconsteffect/Record.ts:507
<K extends string | symbol, B>(key: NoInfer<K>, b: B): <A>(
  self: ReadonlyRecord<K, A>
) => Option.Option<Record<K, A | B>>
<K extends string | symbol, A, B>(
  self: ReadonlyRecord<K, A>,
  key: NoInfer<K>,
  b: B
): Option.Option<Record<K, A | B>>

Replaces the value at an existing key safely and returns the updated record in Option.some.

Details

If the key is not present, returns Option.none() and leaves the record unchanged.

Example (Replacing a value at a key)

import { Record } from "effect"

Record.replace({ a: 1, b: 2, c: 3 }, "a", 10) // Option.some({ a: 10, b: 2, c: 3 })
Record.replace(Record.empty<string>(), "a", 10) // Option.none()
mutations
Source effect/Record.ts:50718 lines
export const replace: {
  <K extends string | symbol, B>(
    key: NoInfer<K>,
    b: B
  ): <A>(self: ReadonlyRecord<K, A>) => Option.Option<Record<K, A | B>>
  <K extends string | symbol, A, B>(
    self: ReadonlyRecord<K, A>,
    key: NoInfer<K>,
    b: B
  ): Option.Option<Record<K, A | B>>
} = dual(
  3,
  <K extends string | symbol, A, B>(
    self: ReadonlyRecord<K, A>,
    key: NoInfer<K>,
    b: B
  ): Option.Option<Record<K, A | B>> => modify(self, key, () => b)
)