Skip to content

Fix error mapping type mismatch in errorMappingFn #46

@michaelbe812

Description

@michaelbe812

Type Safety Issue

There is a type mismatch in the error mapping function configuration that forces unsafe type casting.

Locations

  • src/lib/types/types.ts (RxStatefulConfig.errorMappingFn)
  • src/lib/create-state.ts (handleError function)

Problem

The errorMappingFn is typed as:

errorMappingFn: (error: E) => unknown

However, the error field in RxStatefulWithError is typed as E, but the current code casts the result to any:

// Current problematic implementation
error: config.errorMappingFn ? config.errorMappingFn(error) as any : error

Type Inconsistency

The function returns unknown but we expect type E for the error field, requiring an unsafe cast.

Proposed Solutions

Option 1: Keep error type consistent (Recommended)

errorMappingFn: (error: unknown | E) => E

Option 2: Widen error field type

// In RxStatefulWithError
error: unknown

Recommendation

Option 1 is preferred as it keeps the error type consistent end-to-end and provides better type safety for consumers.

Acceptance Criteria

  • Remove as any cast in error handling
  • Update errorMappingFn signature to return E type
  • Ensure type safety throughout error handling pipeline
  • Update tests to verify type correctness
  • Update documentation if needed

Impact

  • Improves type safety
  • Removes unsafe casting
  • Better developer experience with proper typing

Priority

Medium - Type safety improvement, not a runtime bug

Labels

typescript, type-safety, refactor

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions