Hyperlinkv0.8.0-beta.28

Struct

Struct.renameKeysconsteffect/Struct.ts:467
<
  S extends object,
  const M extends { readonly [K in keyof S]?: PropertyKey }
>(
  mapping: M
): (self: S) => {
  [K in keyof S as K extends keyof M
    ? M[K] extends PropertyKey
      ? M[K]
      : K
    : K]: S[K]
}
<
  S extends object,
  const M extends { readonly [K in keyof S]?: PropertyKey }
>(
  self: S,
  mapping: M
): {
  [K in keyof S as K extends keyof M
    ? M[K] extends PropertyKey
      ? M[K]
      : K
    : K]: S[K]
}

Renames keys in a struct using a static { oldKey: newKey } mapping. Keys not mentioned in the mapping are copied unchanged.

When to use

Use when you need simple, declarative key renaming without custom logic.

Details

For computed key names, use evolveKeys instead.

Example (Renaming keys)

import { pipe, Struct } from "effect"

const result = pipe(
  { firstName: "Alice", lastName: "Smith", age: 30 },
  Struct.renameKeys({ firstName: "first", lastName: "last" })
)
console.log(result) // { first: "Alice", last: "Smith", age: 30 }
Source effect/Struct.ts:46711 lines
export const renameKeys: {
  <S extends object, const M extends { readonly [K in keyof S]?: PropertyKey }>(
    mapping: M
  ): (self: S) => { [K in keyof S as K extends keyof M ? M[K] extends PropertyKey ? M[K] : K : K]: S[K] }
  <S extends object, const M extends { readonly [K in keyof S]?: PropertyKey }>(
    self: S,
    mapping: M
  ): { [K in keyof S as K extends keyof M ? M[K] extends PropertyKey ? M[K] : K : K]: S[K] }
} = dual(2, <S extends object, const M extends { readonly [K in keyof S]?: PropertyKey }>(self: S, mapping: M) => {
  return buildStruct(self, (k, v) => [Object.hasOwn(mapping, k) ? mapping[k]! : k, v])
})
Referenced by 1 symbols