(u: Input): Option.Option<Duration>Decodes a Input value into a Duration safely, returning
Option.none() if decoding fails.
Example (Safely decoding duration inputs)
import { Duration, Option } from "effect"
Duration.fromInput(1000).pipe(Option.map(Duration.toSeconds)) // Some(1)
Duration.fromInput("invalid" as any) // Noneexport const const fromInput: (
u: Input
) => Option.Option<Duration>
Decodes a Input value into a Duration safely, returning
Option.none() if decoding fails.
Example (Safely decoding duration inputs)
import { Duration, Option } from "effect"
Duration.fromInput(1000).pipe(Option.map(Duration.toSeconds)) // Some(1)
Duration.fromInput("invalid" as any) // None
fromInput: (u: Inputu: type Input =
| number
| bigint
| Duration
| "Infinity"
| readonly [seconds: number, nanos: number]
| `${number} nano`
| `${number} nanos`
| `${number} micro`
| `${number} micros`
| `${number} milli`
| `${number} millis`
| `${number} second`
| `${number} seconds`
| `${number} minute`
| `${number} minutes`
| `${number} hour`
| `${number} hours`
| `${number} day`
| `${number} days`
| `${number} week`
| `${number} weeks`
| "-Infinity"
| DurationObject
Valid input types that can be converted to a Duration.
When to use
Use when an API should accept any value that Effect can convert into a
Duration, including existing durations, millisecond numbers, nanosecond
bigints, high-resolution tuples, duration strings, infinity strings, or
duration objects.
Details
String inputs accept values like "10 seconds", "500 millis",
"Infinity", and "-Infinity". Finite fractional values that are
normalized to nanoseconds are rounded to the nearest nanosecond, with ties
away from zero.
Input) => import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<Duration> = import OptionOption.const liftThrowable: <
A extends ReadonlyArray<unknown>,
B
>(
f: (...a: A) => B
) => (...a: A) => Option<B>
Lifts a function that may throw into one that returns an Option.
When to use
Use to wrap exception-throwing APIs (e.g. JSON.parse) for safe usage
Details
- If the function returns normally →
Some with the result
- If the function throws →
None (exception is swallowed)
Example (Lifting JSON.parse)
import { Option } from "effect"
const parse = Option.liftThrowable(JSON.parse)
console.log(parse("1"))
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
console.log(parse(""))
// Output: { _id: 'Option', _tag: 'None' }
liftThrowable(
const fromInputUnsafe: (
input: Input
) => Duration
Decodes a Duration.Input into a Duration.
When to use
Use when the input has already been validated or comes from a trusted source
and throwing is acceptable for invalid duration syntax.
Gotchas
If the input is not a valid Duration.Input, it throws an error.
Example (Decoding duration inputs)
import { Duration } from "effect"
const duration1 = Duration.fromInputUnsafe(1000) // 1000 milliseconds
const duration2 = Duration.fromInputUnsafe("5 seconds")
const duration3 = Duration.fromInputUnsafe("Infinity")
const duration4 = Duration.fromInputUnsafe([2, 500_000_000]) // 2 seconds and 500ms
fromInputUnsafe
)