Overview
The serialization API allows you to convertResult 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. ReturnsErr<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 OkErr<T, E>if value is a valid serialized ErrErr<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 whenResult.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
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.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