data-types

from andrueandersoncs/claude-skill-effect-ts

No description

2 stars0 forksUpdated Jan 25, 2026
npx skills add https://github.com/andrueandersoncs/claude-skill-effect-ts --skill data-types

SKILL.md

Data Types in Effect

Overview

Effect provides immutable, type-safe data structures:

  • Option - Represents optional values (Some/None)
  • Either - Represents success/failure (Right/Left)
  • Cause - Detailed failure information
  • Exit - Effect execution result
  • Data - Value equality for classes
  • Chunk - Immutable indexed sequence
  • Duration - Time spans
  • DateTime - Date/time handling

Option

Represents a value that may or may not exist:

import { Option } from "effect"

// Creating Options
const some = Option.some(42)
const none = Option.none()

// From nullable
const fromNull = Option.fromNullable(maybeNull)

// Pattern matching
const result = Option.match(option, {
  onNone: () => "No value",
  onSome: (value) => `Got: ${value}`
})

// Get with default
const value = Option.getOrElse(option, () => defaultValue)

// Map over Some
const doubled = Option.map(option, (n) => n * 2)

// FlatMap for chaining
const chained = Option.flatMap(option, (n) =>
  n > 0 ? Option.some(n) : Option.none()
)

// Filter
const positive = Option.filter(option, (n) => n > 0)

Option with Effect

const program = Effect.gen(function* () {
  const maybeUser = yield* findUser(id)

  // Convert Option to Effect
  const user = yield* Option.match(maybeUser, {
    onNone: () => Effect.fail(new UserNotFound()),
    onSome: Effect.succeed
  })

  // Or use Effect.fromOption
  const user = yield* maybeUser.pipe(
    Effect.fromOption,
    Effect.mapError(() => new UserNotFound())
  )
})

Either

Represents a value that is either Left (failure) or Right (success):

import { Either } from "effect"

// Creating Either
const right = Either.right(42)
const left = Either.left("error")

// Pattern matching
const result = Either.match(either, {
  onLeft: (error) => `Error: ${error}`,
  onRight: (value) => `Success: ${value}`
})

// Map over Right
const doubled = Either.map(either, (n) => n * 2)

// MapLeft over Left
const mapped = Either.mapLeft(either, (e) => new Error(e))

// MapBoth
const both = Either.mapBoth(either, {
  onLeft: (e) => new Error(e),
  onRight: (n) => n * 2
})

// FlatMap
const chained = Either.flatMap(either, (n) =>
  n > 0 ? Either.right(n) : Either.left("negative")
)

// Get or throw
const value = Either.getOrThrow(either)

Cause

Complete failure information for an Effect:

import { Cause } from "effect"

// Cause variants
Cause.fail(error)      // Expected error
Cause.die(defect)      // Unexpected error (defect)
Cause.interrupt(id)    // Interruption
Cause.empty           // No failure
Cause.sequential(c1, c2) // Sequential failures
Cause.parallel(c1, c2)   // Parallel failures

// Inspecting Cause
Cause.isFailure(cause)
Cause.isDie(cause)
Cause.isInterrupt(cause)

// Extracting errors
const failures = Cause.failures(cause) // Chunk of E
const defects = Cause.defects(cause)   // Chunk of unknown

// Pretty print
const message = Cause.pretty(cause)

Exit

The result of running an Effect:

import { Exit } from "effect"

// Exit variants
Exit.succeed(value)    // Success
Exit.fail(cause)       // Failure with Cause

// Pattern matching
const result = Exit.match(exit, {
  onFailure: (cause) => `Failed: ${Cause.pretty(cause)}`,
  onSuccess: (value) => `Succeeded: ${value}`
})

// Check type
Exit.isSuccess(exit)
Exit.isFailure(exit)

// Extract value
const value = Exit.getOrElse(exit, () => defaultValue)

// Map
const mapped = Exit.map(exit, (a) => a * 2)

Data - Value Equality

Create classes with structural equality:

import { Data } from "effect"

// Tagged class
class Person extends Data.Class<{
  readonly name: string
  readonly age: number
}> {}

const alice1 = new Person({ name: "Alice", age: 30 })
const alice2 = new Person({ name: "Alice", age: 30 })

alice1 === alice2  // false (reference)
Equal.equals(alice1, alice2)  // true (structural)

// Tagged errors (used with Effect.fail)
// NOTE: Data.TaggedError is NOT a Schema - Schema.is() does NOT work on these.
// Use Effect.catchTag or Match.tag for error handling.
class UserNotFound extends Data.TaggedError("UserNotFound")<{
  readonly userId: string
}> {}

// Tagged enum
type Shape = Data.TaggedEnum<{
  Circle: { radius: number }
  Rectangle: { width: number; height: number }
}>
const { Circle, Rectangle } = Data.taggedEnum<Shape>()

const circle = Circle({ radius: 10 })
const rect = Rectangle({ width: 5, height: 3 })

Chunk

Immutable indexed sequence optimized for Effect:

import { Chunk } from "effect"

// Creating
const chunk = Chunk.make(1, 2, 3, 4, 5)
const fromArray = Chunk.fromIterable([1, 2, 3])
const empty = Chunk.empty<number>()

// Operations
const head = Chunk.head(chunk)          // Option<number>
const tail = Chunk.tail(chunk)          // Chunk
const take = Chunk.take(chunk, 2)       // Chunk
const drop = Chunk.drop(chunk, 2)       // Chunk

// Functional operations
const 

...
Read full content

Repository Stats

Stars2
Forks0
LicenseMIT License