Types API Reference
Overview
Comprehensive documentation of all type definitions used in jpgx.
Core Types
Encoder Types
typescript
interface EncodeOptions {
// Compression settings
compressionLevel?: number // 0-9, higher means better compression
compressionStrategy?: 'default' | 'filtered' | 'huffman' | 'rle' | 'fixed'
// Format settings
format?: 'json' | 'binary' | 'text'
encoding?: 'utf8' | 'ascii' | 'base64'
// Performance settings
bufferSize?: number
useStreaming?: boolean
// Validation settings
validateInput?: boolean
strictMode?: boolean
}
interface EncoderResult {
data: string
metadata: {
size: number
format: string
encoding: string
compressionLevel: number
}
}
Decoder Types
typescript
interface DecodeOptions {
// Processing settings
timeout?: number
maxSize?: number
// Validation settings
strictMode?: boolean
validateOutput?: boolean
// Error handling
errorHandling?: 'strict' | 'lenient'
retryAttempts?: number
}
interface DecoderResult {
data: any
metadata: {
originalSize: number
decodedSize: number
format: string
encoding: string
}
}
Error Types
typescript
interface JpgxError extends Error {
code: string
details?: Record<string, unknown>
}
interface ValidationError extends JpgxError {
code: 'VALIDATION_ERROR'
field?: string
value?: unknown
expected?: unknown
}
interface EncodingError extends JpgxError {
code: 'ENCODING_ERROR'
input?: unknown
reason?: string
}
interface DecodingError extends JpgxError {
code: 'DECODING_ERROR'
input?: string
reason?: string
}
Utility Types
typescript
type SupportedFormat = 'json' | 'binary' | 'text'
type SupportedEncoding = 'utf8' | 'ascii' | 'base64'
type CompressionStrategy = 'default' | 'filtered' | 'huffman' | 'rle' | 'fixed'
interface Metadata {
timestamp: number
version: string
format: SupportedFormat
encoding: SupportedEncoding
}
Type Guards
typescript
function isJpgxError(error: unknown): error is JpgxError {
return (
error instanceof Error
&& 'code' in error
&& typeof (error as JpgxError).code === 'string'
)
}
function isValidFormat(format: unknown): format is SupportedFormat {
return typeof format === 'string' && ['json', 'binary', 'text'].includes(format)
}
function isValidEncoding(encoding: unknown): encoding is SupportedEncoding {
return typeof encoding === 'string' && ['utf8', 'ascii', 'base64'].includes(encoding)
}
Usage Examples
Type Checking
typescript
import { isJpgxError, isValidFormat } from 'jpgx'
try {
const result = encode(data)
// Process result
}
catch (error) {
if (isJpgxError(error)) {
// Handle jpgx specific error
console.error(`Error code: ${error.code}`)
}
else {
// Handle other errors
console.error('Unknown error:', error)
}
}
Type Assertions
typescript
import { SupportedEncoding, SupportedFormat } from 'jpgx'
function processData(format: unknown, encoding: unknown) {
if (!isValidFormat(format)) {
throw new Error('Invalid format')
}
if (!isValidEncoding(encoding)) {
throw new Error('Invalid encoding')
}
// Now TypeScript knows the types are correct
return encode(data, { format, encoding })
}
Best Practices
Type Safety
- Always use type guards when dealing with unknown data
- Avoid using
any
type when possible - Use proper type assertions
- Implement proper error handling with typed errors
Type Definitions
- Keep type definitions in a separate file
- Use interfaces for object shapes
- Use type aliases for unions and intersections
- Document complex types with examples
Error Handling
- Use typed error classes
- Implement proper error hierarchies
- Include relevant metadata in errors
- Use type guards for error handling