Skip to main content

Function: fromResult()

fromResult<T, E>(result): Validation<T, E>

Defined in: validation/constructors/index.ts:171

Converts a Result to a Validation.

Ok becomes Valid, Err becomes Invalid with a single error.

Type Parameters

T

T

E

E

Parameters

result

Result<T, E>

The Result to convert

Returns

Validation<T, E>

A Validation with the same value or error

Example

import { ok, err } from 'receta/result'

fromResult(ok(42))
// => Valid(42)

fromResult(err('failed'))
// => Invalid(['failed'])

// Real-world: Convert Result-based parsers to Validation
const parseJSON = <T>(str: string): Validation<T, string> =>
fromResult(Result.tryCatch(() => JSON.parse(str) as T))

See

toResult - for converting Validation to Result