(self: Input): Option.Option<bigint>Gets the duration in nanoseconds safely as an Option<bigint>.
Details
If the duration is infinite, returns Option.none().
Example (Safely reading nanoseconds)
import { Duration, Option } from "effect"
Duration.toNanos(Duration.seconds(1)) // Some(1000000000n)
Duration.toNanos(Duration.infinity) // None
Option.getOrUndefined(Duration.toNanos(Duration.infinity)) // undefinedexport const const toNanos: (
self: Input
) => Option.Option<bigint>
Gets the duration in nanoseconds safely as an Option<bigint>.
Details
If the duration is infinite, returns Option.none().
Example (Safely reading nanoseconds)
import { Duration, Option } from "effect"
Duration.toNanos(Duration.seconds(1)) // Some(1000000000n)
Duration.toNanos(Duration.infinity) // None
Option.getOrUndefined(Duration.toNanos(Duration.infinity)) // undefined
toNanos: (self: Inputself: 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<bigint> = 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 toNanosUnsafe: (
input: Input
) => bigint
Gets the duration in nanoseconds as a bigint.
When to use
Use when the duration is known to be finite and you need the nanosecond value
as a bigint.
Details
Millisecond-backed fractional durations are rounded to the nearest
nanosecond, with ties away from zero.
Gotchas
If the duration is infinite, it throws an error.
Example (Reading nanoseconds unsafely)
import { Duration } from "effect"
const duration = Duration.seconds(2)
const nanos = Duration.toNanosUnsafe(duration)
console.log(nanos) // 2000000000n
// Duration.toNanosUnsafe(Duration.infinity)
// throws Error: "Cannot convert infinite duration to nanos"
toNanosUnsafe)