Files
2026-03-31 12:44:27 +02:00

99 lines
2.7 KiB
Markdown

# Result API Surface
These are ALL available methods for Result. Do not use any method not listed here.
## Types
```typescript
type Result<T, E> = Ok<T> | Err<E>
type ResultPromise<T, E> = Promise<Result<T, E>>
type OkPromise<T> = Promise<Ok<T>>
type ErrPromise<E> = Promise<Err<E>>
type ResultPartitionResult<T, E> = { values: T[]; errors: E[] }
```
## Factory Functions
```typescript
function ok<T>(value: T): Ok<T>
function err<E>(error: E): Err<E>
```
## Ok<T> Instance Methods
```typescript
class Ok<T> {
readonly value: T
isOk(): this is Ok<T>
isErr(): this is never
map<U>(fn: (value: T) => U): Ok<U>
mapErr<F>(fn: (error: never) => F): Ok<T>
flatMap<R extends Result<any, any>>(fn: (value: T) => R): R
flatMapErr(): this
match<U>(onOk: (value: T) => U, onErr: (error: never) => U): U
// Overload 1: type-narrowing predicate
filter<U extends T, F>(predicate: (value: T) => value is U, errorFactory: (value: T) => F): Ok<U> | Err<F>
// Overload 2: boolean predicate
filter<F>(predicate: (value: T) => boolean, errorFactory: (value: T) => F): Ok<T> | Err<F>
toNullable(): T
toUndefined(): T
toString(): string
}
```
## Err<E> Instance Methods
```typescript
class Err<E> {
readonly err: E
isOk(): this is never
isErr(): this is Err<E>
map<U>(): Err<E>
mapErr<F>(fn: (error: E) => F): Err<F>
flatMap(): this
flatMapErr<R extends Result<any, any>>(fn: (error: E) => R): R
match<U>(onOk: (value: never) => U, onErr: (error: E) => U): U
filter(): Err<E>
toNullable(): null
toUndefined(): undefined
toString(): string
}
```
## Result Namespace (Static Methods)
```typescript
Result.from<T, E = unknown>(fn: () => T, errorMap?: (error: unknown) => E): Result<T, E>
Result.isResult<T, E>(value: unknown): value is Result<T, E>
Result.all<T>(results: Ok<T>[]): Ok<T[]>
Result.all<T, E>(results: Result<T, E>[]): Result<T[], E>
Result.partition<T, E>(results: Result<T, E>[]): ResultPartitionResult<T, E>
Result.compact<T, E>(results: Result<T, E>[]): T[]
Result.some<T, E>(results: Result<T, E>[]): boolean
Result.every<T, E>(results: Result<T, E>[]): boolean
```
## ResultPromise Namespace (Async Methods)
```typescript
ResultPromise.from<T, E = unknown>(fn: () => Promise<T>, errorMap?: (error: unknown) => E): ResultPromise<T, E>
ResultPromise.all<T>(promises: Promise<Ok<T>>[]): Promise<Ok<T[]>>
ResultPromise.all<T, E>(promises: Promise<Result<T, E>>[]): Promise<Result<T[], E>>
ResultPromise.partition<T, E>(promises: Promise<Result<T, E>>[]): Promise<ResultPartitionResult<T, E>>
ResultPromise.compact<T, E>(promises: Promise<Result<T, E>>[]): Promise<T[]>
ResultPromise.some<T, E>(promises: Promise<Result<T, E>>[]): Promise<boolean>
ResultPromise.every<T, E>(promises: Promise<Result<T, E>>[]): Promise<boolean>
```