Hyperlinkv0.8.0-beta.28

Tuple

Tuple.renameIndicesconsteffect/Tuple.ts:334
<
  const T extends ReadonlyArray<unknown>,
  const M extends { readonly [I in keyof T]?: `${keyof T & string}` }
>(
  mapping: M
): (self: T) => {
  [I in keyof T]: I extends keyof M
    ? M[I] extends keyof T
      ? T[M[I]]
      : T[I]
    : T[I]
}
<
  const T extends ReadonlyArray<unknown>,
  const M extends { readonly [I in keyof T]?: `${keyof T & string}` }
>(
  self: T,
  mapping: M
): {
  [I in keyof T]: I extends keyof M
    ? M[I] extends keyof T
      ? T[M[I]]
      : T[I]
    : T[I]
}

Renames tuple indices by providing an array of stringified source indices. Each position in the array specifies which index to read from (e.g., ["2", "1", "0"] reverses a 3-element tuple).

When to use

Use to reorder tuple elements while preserving index-specific types.

Details

The mapping returns a tuple in the requested index order.

Gotchas

The mapping uses stringified source indices, not arbitrary names.

Example (Swapping elements)

import { pipe, Tuple } from "effect"

const result = pipe(
  Tuple.make("a", "b", "c"),
  Tuple.renameIndices(["2", "1", "0"])
)
console.log(result) // ["c", "b", "a"]
Index utilitiesevolve
Source effect/Tuple.ts:33417 lines
export const renameIndices: {
  <const T extends ReadonlyArray<unknown>, const M extends { readonly [I in keyof T]?: `${keyof T & string}` }>(
    mapping: M
  ): (self: T) => { [I in keyof T]: I extends keyof M ? M[I] extends keyof T ? T[M[I]] : T[I] : T[I] }
  <const T extends ReadonlyArray<unknown>, const M extends { readonly [I in keyof T]?: `${keyof T & string}` }>(
    self: T,
    mapping: M
  ): { [I in keyof T]: I extends keyof M ? M[I] extends keyof T ? T[M[I]] : T[I] : T[I] }
} = dual(
  2,
  <const T extends ReadonlyArray<unknown>, const M extends { readonly [I in keyof T]?: `${keyof T & string}` }>(
    self: T,
    mapping: M
  ) => {
    return self.map((e, i) => mapping[i] !== undefined ? self[mapping[i]] : e)
  }
)