Skip to main content Link Search Menu Expand Document (external link)

Struct overview

This module provides utility functions for working with structs in TypeScript.

Added in v2.0.0


Table of contents


combinators

getEquivalence

Given a struct of Equivalences returns a new Equivalence that compares values of a struct by applying each Equivalence to the corresponding property of the struct.

Alias of {@link Equivalence.struct}.

Signature

export declare const getEquivalence: <R extends Record<string, Equivalence.Equivalence<any>>>(
  isEquivalents: R
) => Equivalence.Equivalence<{ readonly [K in keyof R]: [R[K]] extends [Equivalence.Equivalence<infer A>] ? A : never }>

Example

import { Struct, String, Number } from "effect"

const PersonEquivalence = Struct.getEquivalence({
  name: String.Equivalence,
  age: Number.Equivalence
})

assert.deepStrictEqual(PersonEquivalence({ name: "John", age: 25 }, { name: "John", age: 25 }), true)
assert.deepStrictEqual(PersonEquivalence({ name: "John", age: 25 }, { name: "John", age: 40 }), false)

Added in v2.0.0

getOrder

This function creates and returns a new Order for a struct of values based on the given Orders for each property in the struct.

Alias of {@link order.struct}.

Signature

export declare const getOrder: <R extends { readonly [x: string]: order.Order<any> }>(
  fields: R
) => order.Order<{ [K in keyof R]: [R[K]] extends [order.Order<infer A>] ? A : never }>

Added in v2.0.0

utils

evolve

Transforms the values of a Struct provided a transformation function for each key. If no transformation function is provided for a key, it will return the origional value for that key.

Signature

export declare const evolve: {
  <O, T>(t: PartialTransform<O, T>): (obj: O) => Transformed<O, T>
  <O, T>(obj: O, t: PartialTransform<O, T>): Transformed<O, T>
}

Example

import { pipe, Struct } from "effect"

assert.deepStrictEqual(
  pipe(
    { a: "a", b: 1, c: 3 },
    Struct.evolve({
      a: (a) => a.length,
      b: (b) => b * 2
    })
  ),
  { a: 1, b: 2, c: 3 }
)

Added in v2.0.0

get

Retrieves the value associated with the specified key from a struct.

Signature

export declare const get: <K extends PropertyKey>(
  key: K
) => <S extends { [P in K]?: any }>(s: S) => MatchRecord<S, S[K] | undefined, S[K]>

Example

import { pipe, Struct } from "effect"

const value = pipe({ a: 1, b: 2 }, Struct.get("a"))

assert.deepStrictEqual(value, 1)

Added in v2.0.0

keys

Retrieves the object keys that are strings in a typed manner

Signature

export declare const keys: <T extends {}>(o: T) => Array<keyof T & string>

Example

import { Struct } from "effect"

const symbol: unique symbol = Symbol()

const value = {
  a: 1,
  b: 2,
  [symbol]: 3
}

const keys: Array<"a" | "b"> = Struct.keys(value)

assert.deepStrictEqual(keys, ["a", "b"])

Added in v3.6.0

omit

Create a new object by omitting properties of an existing object.

Signature

export declare const omit: {
  <Keys extends Array<PropertyKey>>(
    ...keys: Keys
  ): <S extends { [K in Keys[number]]?: any }>(s: S) => Simplify<Omit<S, Keys[number]>>
  <S extends object, Keys extends Array<keyof S>>(s: S, ...keys: Keys): Simplify<Omit<S, Keys[number]>>
}

Example

import { pipe, Struct } from "effect"

assert.deepStrictEqual(pipe({ a: "a", b: 1, c: true }, Struct.omit("c")), { a: "a", b: 1 })
assert.deepStrictEqual(Struct.omit({ a: "a", b: 1, c: true }, "c"), { a: "a", b: 1 })

Added in v2.0.0

pick

Create a new object by picking properties of an existing object.

Signature

export declare const pick: {
  <Keys extends Array<PropertyKey>>(
    ...keys: Keys
  ): <S extends { [K in Keys[number]]?: any }>(
    s: S
  ) => MatchRecord<S, { [K in Keys[number]]?: S[K] }, Simplify<Pick<S, Keys[number]>>>
  <S extends object, Keys extends Array<keyof S>>(
    s: S,
    ...keys: Keys
  ): MatchRecord<S, { [K in Keys[number]]?: S[K] }, Simplify<Pick<S, Keys[number]>>>
}

Example

import { pipe, Struct } from "effect"

assert.deepStrictEqual(pipe({ a: "a", b: 1, c: true }, Struct.pick("a", "b")), { a: "a", b: 1 })
assert.deepStrictEqual(Struct.pick({ a: "a", b: 1, c: true }, "a", "b"), { a: "a", b: 1 })

Added in v2.0.0