Hyperlinkv0.8.0-beta.28

Struct

Struct.evolveconsteffect/Struct.ts:324
<S extends object, E extends Evolver<S>>(e: E): (self: S) => Evolved<S, E>
<S extends object, E extends Evolver<S>>(self: S, e: E): Evolved<S, E>

Transforms values of a struct selectively using per-key functions. Keys without a corresponding function are copied unchanged.

When to use

Use when you want to update specific fields while keeping the rest intact.

Details

Each transform function receives the current value and returns the new value; the return type can differ from the input type.

Example (Transforming selected values)

import { pipe, Struct } from "effect"

const result = pipe(
  { name: "alice", age: 30, active: true },
  Struct.evolve({
    name: (s) => s.toUpperCase(),
    age: (n) => n + 1
  })
)
console.log(result) // { name: "ALICE", age: 31, active: true }
Source effect/Struct.ts:3249 lines
export const evolve: {
  <S extends object, E extends Evolver<S>>(e: E): (self: S) => Evolved<S, E>
  <S extends object, E extends Evolver<S>>(self: S, e: E): Evolved<S, E>
} = dual(
  2,
  <S extends object, E extends Evolver<S>>(self: S, e: E): Evolved<S, E> => {
    return buildStruct(self, (k, v) => [k, Object.hasOwn(e, k) ? (e as any)[k](v) : v])
  }
)