Skip to content

Struct

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

8 exports Added in v2.0.0 Source

Combinators

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 Equivalence.struct.

Signature

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

Example

import * as assert from "node:assert"
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,
)

getOrder

Added in v2.0.0 Source

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 order.struct.

Signature

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

Other

entries

Added in v3.17.0 Source

Retrieves the entries (key-value pairs) of an object, where keys are strings, in a type-safe manner. Symbol keys are excluded from the result.

Signature

declare function entries<R>(obj: R): Array<[keyof R & string, R[keyof R & string]]>;

evolve

Added in v2.0.0 Source

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 original value for that key.

Signature

declare const evolve: {
  <O, T>(
    t: PartialTransform<O, T>,
  ): (obj: O) => {
    [K in string | number | symbol]: K extends keyof T
      ? T[K] extends (...a: any) => any
        ? ReturnType<any[any]>
        : O[K]
      : O[K];
  };
  <O, T>(
    obj: O,
    t: PartialTransform<O, T>,
  ): {
    [K in string | number | symbol]: K extends keyof T
      ? T[K] extends (...a: any) => any
        ? ReturnType<any[any]>
        : O[K]
      : O[K];
  };
};

Example

import * as assert from "node:assert"
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 },
)

get

Added in v2.0.0 Source

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

Signature

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

keys

Added in v3.6.0 Source

Retrieves the object keys that are strings in a typed manner

Signature

declare function keys<T extends {}>(o: T): Array<keyof T & string>;

omit

Added in v2.0.0 Source

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

Signature

declare const omit: {
  <Keys extends Array<PropertyKey>>(
    ...keys: Keys
  ): <S extends { [K in PropertyKey]: 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 * as assert from "node:assert"
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 })

pick

Added in v2.0.0 Source

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

Signature

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

Example

import * as assert from "node:assert"
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 })