<K extends string, A, B>(self: ReadonlyRecord<K, Result<B, A>>): [
Record<ReadonlyRecord.NonLiteralKey<K>, A>,
Record<ReadonlyRecord.NonLiteralKey<K>, B>
]Partitions a record of Result values into two separate records,
one with the Err values and one with the Ok values.
Example (Separating Result values)
import { Record, Result } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(
Record.separate({ a: Result.fail("e"), b: Result.succeed(1) }),
[{ a: "e" }, { b: 1 }]
)export const const separate: <K extends string, A, B>(
self: ReadonlyRecord<K, Result<B, A>>
) => [
Record<ReadonlyRecord.NonLiteralKey<K>, A>,
Record<ReadonlyRecord.NonLiteralKey<K>, B>
]
Partitions a record of Result values into two separate records,
one with the Err values and one with the Ok values.
Example (Separating Result values)
import { Record, Result } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(
Record.separate({ a: Result.fail("e"), b: Result.succeed(1) }),
[{ a: "e" }, { b: 1 }]
)
separate: <function (type parameter) K in <K extends string, A, B>(self: ReadonlyRecord<K, Result<B, A>>): [Record<ReadonlyRecord.NonLiteralKey<K>, A>, Record<ReadonlyRecord.NonLiteralKey<K>, B>]K extends string, function (type parameter) A in <K extends string, A, B>(self: ReadonlyRecord<K, Result<B, A>>): [Record<ReadonlyRecord.NonLiteralKey<K>, A>, Record<ReadonlyRecord.NonLiteralKey<K>, B>]A, function (type parameter) B in <K extends string, A, B>(self: ReadonlyRecord<K, Result<B, A>>): [Record<ReadonlyRecord.NonLiteralKey<K>, A>, Record<ReadonlyRecord.NonLiteralKey<K>, B>]B>(
self: ReadonlyRecord<K, Result<B, A>>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, B>(self: ReadonlyRecord<K, Result<B, A>>): [Record<ReadonlyRecord.NonLiteralKey<K>, A>, Record<ReadonlyRecord.NonLiteralKey<K>, B>]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) B in <K extends string, A, B>(self: ReadonlyRecord<K, Result<B, A>>): [Record<ReadonlyRecord.NonLiteralKey<K>, A>, Record<ReadonlyRecord.NonLiteralKey<K>, B>]B, function (type parameter) A in <K extends string, A, B>(self: ReadonlyRecord<K, Result<B, A>>): [Record<ReadonlyRecord.NonLiteralKey<K>, A>, Record<ReadonlyRecord.NonLiteralKey<K>, B>]A>>
) => [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, B>(self: ReadonlyRecord<K, Result<B, A>>): [Record<ReadonlyRecord.NonLiteralKey<K>, A>, Record<ReadonlyRecord.NonLiteralKey<K>, B>]K>, function (type parameter) A in <K extends string, A, B>(self: ReadonlyRecord<K, Result<B, A>>): [Record<ReadonlyRecord.NonLiteralKey<K>, A>, Record<ReadonlyRecord.NonLiteralKey<K>, B>]A>, 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, B>(self: ReadonlyRecord<K, Result<B, A>>): [Record<ReadonlyRecord.NonLiteralKey<K>, A>, Record<ReadonlyRecord.NonLiteralKey<K>, B>]K>, function (type parameter) B in <K extends string, A, B>(self: ReadonlyRecord<K, Result<B, A>>): [Record<ReadonlyRecord.NonLiteralKey<K>, A>, Record<ReadonlyRecord.NonLiteralKey<K>, B>]B>] = const partition: {
<K extends string, A, B, C>(
f: (input: A, key: K) => Result<C, B>
): (
self: ReadonlyRecord<K, A>
) => [
left: Record<
ReadonlyRecord.NonLiteralKey<K>,
B
>,
right: Record<
ReadonlyRecord.NonLiteralKey<K>,
C
>
]
<K extends string, A, B, C>(
self: ReadonlyRecord<K, A>,
f: (input: A, key: K) => Result<C, B>
): [
left: Record<
ReadonlyRecord.NonLiteralKey<K>,
B
>,
right: Record<
ReadonlyRecord.NonLiteralKey<K>,
C
>
]
}
partition(import identityidentity)