Skip to content

TS Utils

TypeScript utilities for error handling, async operations, and type safety.

Available Utilities

Result Pattern

Error handling utilities using the Result pattern for type-safe error handling.

ts
import { ok, err, $result, $trap, $try } from '#appkit/tools'

// Create results
const success = ok(data, $code.ok)
const failure = err(error, $code.badRequest)

// Handle results
$result(result).when({
  ok: (data) => console.log(data),
  err: (error) => console.error(error)
})

// Safe async operations
const result = await $trap(async () => {
  return ok(await riskyOperation(), $code.ok)
})

// Safe sync operations  
const syncResult = $try(() => {
  return JSON.parse(jsonString)
})

Find out more: Result Documentation | $result Documentation

Task Type

Type alias for Promise-based Result operations.

ts
import type { Task } from '#appkit/tools'

// Task<Ok, Err> = Promise<Result<Ok, Err>>
function fetchUser(): Task<User, string> {
  return $trap(async () => {
    const user = await api.getUser()
    return ok(user, $code.ok)
  })
}

Find out more: Task Documentation

Error Handling Utilities

Advanced error handling with stack traces and debugging support.

ts
import { $trap, $try } from '#appkit/tools'

// Async error trapping with detailed error info
const result = await $trap(() => asyncOperation())

// Sync error trapping
const syncResult = $try(() => riskyOperation())

Find out more: $trap Documentation | $try Documentation

Utility Types

Common TypeScript utility types.

ts
import type { id } from '#appkit/tools'

// Simple ID type alias
const userId: id = "user-123"

Utilities in Development

The following utilities are in development:

  • Option Pattern - Optional value handling (in development)
  • Union Utilities - Union type manipulation helpers (planned)

See Option Documentation for the planned API.

Integration

These utilities integrate seamlessly with other appkit tools:

  • $assert - Uses Result pattern for validation
  • $email - Uses Task type for async operations
  • $code - Provides error codes for Result pattern