Automatic Type Inference
When you create a decoder, TypeScript automatically infers what type it will produce:Object Type Inference
For objects, TypeScript infers the complete structure:optional() fields become optional properties with the ? modifier.
Array Type Inference
Arrays infer their element types:Nested Structure Inference
TypeScript correctly infers deeply nested structures:The DecoderType Helper
When you need to extract the type from a decoder as a standalone type, use theDecoderType helper:
Using DecoderType with typeof
Thetypeof operator is crucial when using DecoderType:
Type Inference with Transformations
When you transform a decoder, TypeScript tracks the type changes:Type Inference with Refinements
Refinements can narrow types when using type predicates:Union Type Inference
Union decoders correctly infer union types:Complex Inference Examples
Combining multiple features:Benefits of Type Inference
- Single Source of Truth: Define the validation logic once, get types automatically
- No Type Duplication: Avoid manually writing interface definitions that mirror your decoders
- Automatic Updates: Change the decoder, and TypeScript updates types everywhere
- Type Safety: Guaranteed alignment between runtime validation and compile-time types
- Better Refactoring: TypeScript catches breaking changes when you modify decoders
