Skip to main content

Overview

The serialization API allows you to convert Result instances to plain JavaScript objects for network transfer, RPC calls, or server actions, then reconstruct them back into proper Ok or Err instances. This is essential for:
  • Next.js Server Actions
  • tRPC procedures
  • API responses
  • Any scenario where Result instances cross process boundaries

Result.serialize

Converts a Result instance into a plain object that can be safely serialized to JSON.

Signature

Result<T, E>
required
The Result instance to serialize
SerializedResult<T, E>
A plain object representing the Result:
  • { status: "ok", value: T } for Ok
  • { status: "error", error: E } for Err

Usage Examples


Result.deserialize

Reconstructs a Result instance from a serialized plain object. Returns Err<ResultDeserializationError> if the input is invalid.

Signature

unknown
required
The value to deserialize (typically from JSON)
Result<T, E | ResultDeserializationError>
  • Ok<T, E> if value is a valid serialized Ok
  • Err<T, E> if value is a valid serialized Err
  • Err<T, ResultDeserializationError> if value is invalid

Usage Examples

Result.deserialize is type-safe but cannot validate the actual runtime shape of T or E. Consider using a schema validator like Zod for runtime validation of the payload.

SerializedResult Type

The plain object structure representing a serialized Result.

Type Definition

object
Represents a serialized success result
object
Represents a serialized error result

Usage Example


ResultDeserializationError

Error returned when Result.deserialize receives invalid input.

Class Definition

"ResultDeserializationError"
required
Tagged error discriminator
string
required
Error message describing the deserialization failure
unknown
required
The invalid value that failed to deserialize

Type Guard

Static type guard method to check if a value is a ResultDeserializationError instance.

Usage Examples


Complete Example: Server Action + Client


Best Practices

Always validate deserialized data - Result.deserialize only validates the Result structure, not the actual payload types. Use schema validators like Zod for production applications.
Serialize at boundaries - Only serialize Results when crossing process boundaries (server actions, API routes). Keep them as proper instances within the same process.
Type consistency - Ensure the generic types used in Result.deserialize match those used in Result.serialize on the server side.

See Also

Result Core

Learn about Result, Ok, and Err classes

Type Utilities

Extract types with InferOk and InferErr

Error Handling

TaggedError and error patterns