<K extends string, A, E>(self: ReadonlyRecord<K, Result<A, E>>): Record<
string,
A
>Returns a new record containing only the Ok values from a record of
Result values, preserving the original keys.
Example (Extracting Result successes)
import { Record, Result } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(
Record.getSuccesses({
a: Result.succeed(1),
b: Result.fail("err"),
c: Result.succeed(2)
}),
{ a: 1, c: 2 }
)export const const getSuccesses: <
K extends string,
A,
E
>(
self: ReadonlyRecord<K, Result<A, E>>
) => Record<string, A>
Returns a new record containing only the Ok values from a record of
Result values, preserving the original keys.
Example (Extracting Result successes)
import { Record, Result } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(
Record.getSuccesses({
a: Result.succeed(1),
b: Result.fail("err"),
c: Result.succeed(2)
}),
{ a: 1, c: 2 }
)
getSuccesses = <function (type parameter) K in <K extends string, A, E>(self: ReadonlyRecord<K, Result<A, E>>): Record<string, A>K extends string, function (type parameter) A in <K extends string, A, E>(self: ReadonlyRecord<K, Result<A, E>>): Record<string, A>A, function (type parameter) E in <K extends string, A, E>(self: ReadonlyRecord<K, Result<A, E>>): Record<string, A>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<string, A>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<string, A>A, function (type parameter) E in <K extends string, A, E>(self: ReadonlyRecord<K, Result<A, E>>): Record<string, A>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<string, function (type parameter) A in <K extends string, A, E>(self: ReadonlyRecord<K, Result<A, E>>): Record<string, A>A> => {
const const out: Record<string, A>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) A in <K extends string, A, E>(self: ReadonlyRecord<K, Result<A, E>>): Record<string, A>A> = const empty: <string, A>() => Record<
string,
A
>
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 isSuccess: <A, E>(
self: Result<A, E>
) => self is Success<A, E>
Checks whether a Result is a Success.
When to use
Use to narrow a known Result to the Success variant.
Details
- Acts as a TypeScript type guard, narrowing to
Success<A, E>
- After narrowing, you can access
.success to read the value
Example (Narrowing to success)
import { Result } from "effect"
const result = Result.succeed(42)
if (Result.isSuccess(result)) {
console.log(result.success)
// Output: 42
}
isSuccess(const value: Result<A, E>value)) {
const out: Record<string, A>out[const key: K extends stringkey] = const value: R.Success<A, E>const value: {
_tag: "Success";
_op: "Success";
success: A;
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.Success<A, E>.success: Asuccess
}
}
return const out: Record<string, A>out
}