TypeScript Patterns
Voltaire uses specific TypeScript patterns for type safety, tree-shaking, and API consistency.Branded Types
All primitives are brandedUint8Arrays - zero runtime overhead, full type safety.
Why Branded Types?
- Type Safety: Can’t accidentally pass
HashwhereAddressexpected - Zero Overhead: Just TypeScript - no runtime checks
- Uint8Array Base: Works with all binary APIs natively
- Self-Documenting: Types describe exact byte lengths
File Organization
Each primitive follows this structure:Why .js for Implementation?
Implementation files use.js with JSDoc types:
- No compilation step for runtime code
- JSDoc provides type checking
- Better tree-shaking
- Faster builds
Namespace Pattern
Functions are exported both internally and wrapped:Usage
Why Dual Exports?
| Export | Use Case | Performance |
|---|---|---|
toHex() | General usage | Conversion overhead |
_toHex() | Hot paths, already have branded type | Zero overhead |
Constructor Pattern
Main Constructor
The primary constructor is just the type name:Named Constructors
For specific input types:Type Narrowing
Input Types
Define input types for flexible function signatures:The from Function
Central converter that handles all inputs:
Validation Pattern
Type Guards
Validation Functions
Error Handling
Custom Errors
Usage in Functions
Testing Pattern
Tests in separate.test.ts files using Vitest:
Index File Template
Completeindex.ts structure:

