<K extends string, A, E>(self: ReadonlyRecord<K, Result<A, E>>): Record<
ReadonlyRecord.NonLiteralKey<K>,
E
>Returns a new record containing only the Err values from a record of
Result values, preserving the original keys.
Example (Extracting Result failures)
import { Record, Result } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(
Record.getFailures({
a: Result.succeed(1),
b: Result.fail("err"),
c: Result.succeed(2)
}),
{ b: "err" }
)export const const getFailures: <
K extends string,
A,
E
>(
self: ReadonlyRecord<K, Result<A, E>>
) => Record<ReadonlyRecord.NonLiteralKey<K>, E>
Returns a new record containing only the Err values from a record of
Result values, preserving the original keys.
Example (Extracting Result failures)
import { Record, Result } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(
Record.getFailures({
a: Result.succeed(1),
b: Result.fail("err"),
c: Result.succeed(2)
}),
{ b: "err" }
)
getFailures = <function (type parameter) K in <K extends string, A, E>(self: ReadonlyRecord<K, Result<A, E>>): Record<ReadonlyRecord.NonLiteralKey<K>, E>K extends string, function (type parameter) A in <K extends string, A, E>(self: ReadonlyRecord<K, Result<A, E>>): Record<ReadonlyRecord.NonLiteralKey<K>, E>A, function (type parameter) E in <K extends string, A, E>(self: ReadonlyRecord<K, Result<A, E>>): Record<ReadonlyRecord.NonLiteralKey<K>, E>E>(
self: ReadonlyRecord<K, Result<A, E>>self: type ReadonlyRecord<in out K extends string | symbol, out A> = { readonly [P in K]: A; }Represents a readonly record with keys of type K and values of type A.
This is the foundational type for immutable key-value mappings in Effect.
Example (Defining a readonly record type)
import type { Record } from "effect"
// Creating a readonly record type
type UserRecord = Record.ReadonlyRecord<"name" | "age", string | number>
const user: UserRecord = {
name: "John",
age: 30
}
Namespace containing utility types for working with readonly records.
These types help with type-level operations on record keys and values.
Example (Using readonly record helper types)
import type { Record } from "effect"
// Using NonLiteralKey to convert literal keys to generic types
type GenericKey = Record.ReadonlyRecord.NonLiteralKey<"foo" | "bar"> // string
// Using IntersectKeys to find common keys between record types
type CommonKeys = Record.ReadonlyRecord.IntersectKeys<"a" | "b", "b" | "c"> // "b"
ReadonlyRecord<function (type parameter) K in <K extends string, A, E>(self: ReadonlyRecord<K, Result<A, E>>): Record<ReadonlyRecord.NonLiteralKey<K>, E>K, type Result<A, E = never> = R.Success<A, E> | R.Failure<A, E>A value that is either Success<A, E> or Failure<A, E>.
When to use
Use when both success and failure should remain available as data and
Option would lose failure information.
Details
- Use
succeed
/
fail
to construct
- Use
match
to fold both branches
- Use
isSuccess
/
isFailure
to narrow the type
E defaults to never, so Result<number> means a result that cannot fail.
Example (Creating and matching a Result)
import { Result } from "effect"
const success = Result.succeed(42)
const failure = Result.fail("something went wrong")
const message = Result.match(success, {
onSuccess: (value) => `Success: ${value}`,
onFailure: (error) => `Error: ${error}`
})
console.log(message)
// Output: "Success: 42"
Namespace containing type-level utilities for extracting the inner types
of a Result.
Example (Extracting inner types)
import type { Result } from "effect"
type R = Result.Result<number, string>
// number
type A = Result.Result.Success<R>
// string
type E = Result.Result.Failure<R>
Result<function (type parameter) A in <K extends string, A, E>(self: ReadonlyRecord<K, Result<A, E>>): Record<ReadonlyRecord.NonLiteralKey<K>, E>A, function (type parameter) E in <K extends string, A, E>(self: ReadonlyRecord<K, Result<A, E>>): Record<ReadonlyRecord.NonLiteralKey<K>, E>E>>
): type Record<K extends keyof any, T> = {
[P in K]: T
}
Construct a type with a set of properties K of type T
Record<ReadonlyRecord.type ReadonlyRecord<in out K extends string | symbol, out A>.NonLiteralKey<K extends string | symbol> = K extends string ? ReadonlyRecord.IsFiniteString<K> extends true ? string : K : symbolRepresents a type that converts literal string keys to generic string type and symbol keys to generic symbol type.
This is useful for maintaining type safety while allowing flexible key types in record operations.
Example (Converting literal keys to non-literal keys)
import type { Record } from "effect"
// For literal string keys, this becomes 'string'
type Example1 = Record.ReadonlyRecord.NonLiteralKey<"foo" | "bar"> // string
// For symbol keys, this becomes 'symbol'
type Example2 = Record.ReadonlyRecord.NonLiteralKey<symbol> // symbol
NonLiteralKey<function (type parameter) K in <K extends string, A, E>(self: ReadonlyRecord<K, Result<A, E>>): Record<ReadonlyRecord.NonLiteralKey<K>, E>K>, function (type parameter) E in <K extends string, A, E>(self: ReadonlyRecord<K, Result<A, E>>): Record<ReadonlyRecord.NonLiteralKey<K>, E>E> => {
const const out: Record<string, E>out: type Record<K extends keyof any, T> = {
[P in K]: T
}
Construct a type with a set of properties K of type T
Record<string, function (type parameter) E in <K extends string, A, E>(self: ReadonlyRecord<K, Result<A, E>>): Record<ReadonlyRecord.NonLiteralKey<K>, E>E> = const empty: <string, E>() => Record<
string,
E
>
Creates a new, empty record.
Example (Creating an empty record)
import { Record } from "effect"
// Create an empty record
const emptyRecord = Record.empty<string, number>()
console.log(emptyRecord) // {}
// The type ensures type safety for future operations
const withValue = Record.set(emptyRecord, "count", 42)
console.log(withValue) // { count: 42 }
empty()
for (const const key: K extends stringkey of const keys: <
K extends string | symbol,
A
>(
self: ReadonlyRecord<K, A>
) => Array<K & string>
Retrieves the keys of a given record as an array.
Example (Getting record keys)
import { Record } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(Record.keys({ a: 1, b: 2, c: 3 }), ["a", "b", "c"])
keys(self: ReadonlyRecord<K, Result<A, E>>self)) {
const const value: ReadonlyRecord<
K,
Result<A, E>
>[K]
value = self: ReadonlyRecord<K, Result<A, E>>self[const key: K extends stringkey]
if (import RR.const isFailure: <A, E>(
self: Result<A, E>
) => self is Failure<A, E>
Checks whether a Result is a Failure.
When to use
Use to narrow a known Result to the Failure variant.
Details
- Acts as a TypeScript type guard, narrowing to
Failure<A, E>
- After narrowing, you can access
.failure to read the error value
Example (Narrowing to failure)
import { Result } from "effect"
const result = Result.fail("oops")
if (Result.isFailure(result)) {
console.log(result.failure)
// Output: "oops"
}
isFailure(const value: Result<A, E>value)) {
const out: Record<string, E>out[const key: K extends stringkey] = const value: R.Failure<A, E>const value: {
_tag: "Failure";
_op: "Failure";
failure: E;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
value.Failure<A, E>.failure: Efailure
}
}
return const out: Record<string, E>out
}