Helpers

Result

Minimal explicit success/failure values used across the public API.

Result is intentionally small in this library:

  • construction failures stay explicit in the return type
  • no helper ever throws to surface ordinary validation failure
  • the public surface stops at simple creation, refinement, folding, and structural aggregation helpers

Use this when an operation can fail because of runtime data, but that failure should remain visible and typed instead of being hidden behind exceptions or implicit coercion.

In practical ECS code this is the shared language for construction-time validation and fallible lookups. It keeps ordinary failure in the type surface instead of pushing it into exceptions.

Examples

// Validate several authored inputs and keep every failure explicit.
const built = Result.all({
  position: Vector2.result({ x: 4, y: 8 }),
  size: Size2.result({ width: 16, height: 16 })
})

Refinements

isSuccess

Narrows one Result to its successful branch.

isFailure

Narrows one Result to its failed branch.

Constructors

Small helpers that create explicit success or failure values.

Refinements

Type guards that narrow one result to its successful or failed branch.

isSuccess

Source

Narrows one Result to its successful branch.

Operations

Pure helpers that fold or aggregate explicit result values.

match

Source

Folds one Result into a plain value.

Use this when you are leaving the Result shape and want one explicit place to handle both branches.

const label = Result.match(Vector2.result({ x: 0, y: 0 }), {
  onSuccess: () => "ok",
  onFailure: () => "invalid"
})

all

Source

Aggregates several independent results into one explicit result.

Tuple input preserves tuple ordering in both success and failure shapes. Record input preserves the original keys.

The first failure stays explicit in the returned error structure rather than throwing or silently dropping invalid entries.

const geometry = Result.all({
  position: Vector2.result({ x: 10, y: 20 }),
  size: Size2.result({ width: 32, height: 16 })
})