The Result Type
TheResult<T, E> type is a discriminated union that explicitly represents success or failure:
Annotation, so DecodeResult<T> is an alias for Result<T, Annotation>.
Handling Results Safely
Use the.decode() method to get a Result that never throws:
ok field is a discriminant that lets TypeScript narrow the type:
- When
ok === true, you can accessvalue - When
ok === false, you can accesserror
Try/Catch with .verify()
For simpler control flow, use.verify() which throws on failure:
Error instance with a formatted message.
Optional Values with .value()
When you don’t need the error message, use.value() which returns undefined on failure:
Understanding Annotations
When validation fails, decoders create anAnnotation that captures:
- The actual value that failed
- An error message explaining why
- For complex types, nested annotations for each failing field
Scalar Annotations
For simple value failures:Object Annotations
For object validation failures, annotations include field-level errors:Array Annotations
For array validation failures:Error Formatting
Decoders provides built-in formatters to create human-readable error messages:Inline Format (Default)
TheformatInline formatter shows the invalid value with error annotations:
.verify().
Short Format
TheformatShort formatter provides concise, line-by-line error summaries:
