DurationFromNanosType-level representation of DurationFromNanos.
export interface DurationFromNanos extends interface decodeTo<To extends Constraint, From extends Constraint, RD = never, RE = never>Creates a schema that transforms from a source schema to a target schema.
When to use
Use when decoding should change the schema's decoded type or encoded shape,
with an optional custom bidirectional transformation.
Details
Call it with the target schema to and then pipe the source schema from
into the returned function. The resulting schema decodes from
From["Encoded"] to To["Type"] and encodes from To["Type"] back to
From["Encoded"].
When no transformation is provided, SchemaTransformation.passthrough() is
used, so From["Type"] must already be compatible with To["Encoded"].
The resulting schema combines decoding and encoding services from both
schemas and any custom transformation.
Gotchas
In a custom transformation, decode maps From["Type"] to To["Encoded"]
and is used on the encoding path, while encode maps To["Encoded"] to
From["Type"] and is used on the decoding path.
Example (Transforming strings to numbers with a schema transformation)
import { Schema, SchemaGetter } from "effect"
const NumberFromString = Schema.String.pipe(
Schema.decodeTo(
Schema.Number,
{
decode: SchemaGetter.transform((s) => Number(s)),
encode: SchemaGetter.transform((n) => String(n))
}
)
)
const result = Schema.decodeUnknownSync(NumberFromString)("123")
// result: 123
Type-level representation returned by
decodeTo
.
decodeTo<Duration, BigInt> {
readonly "Rebuild": DurationFromNanos
}
const const bigint0: bigintbigint0 = module globalThisglobalThis.var BigInt: BigIntConstructor
;(value: bigint | boolean | number | string) =>
bigint
BigInt(0)
/**
* Schema that decodes a non-negative `bigint` into a
* `Duration`, treating the bigint as nanoseconds.
*
* **Details**
*
* Decoding:
* A non-negative `bigint` representing nanoseconds is decoded as a `Duration`.
*
* Encoding:
* Finite durations are encoded as a non-negative `bigint` number of nanoseconds.
* Encoding fails when the duration cannot be represented as nanoseconds, such as
* `Duration.infinity`.
*
* @category Duration
* @since 3.10.0
*/
export const const DurationFromNanos: DurationFromNanosconst DurationFromNanos: {
Rebuild: DurationFromNanos;
Type: To["Type"];
Encoded: From["Encoded"];
DecodingServices: To["DecodingServices"] | From["DecodingServices"] | RD;
EncodingServices: To["EncodingServices"] | From["EncodingServices"] | RE;
Iso: To["Iso"];
from: From;
to: To;
ast: Ast;
annotate: (annotations: Annotations.Bottom<Duration_.Duration, readonly []>) => DurationFromNanos;
annotateKey: (annotations: Annotations.Key<Duration_.Duration>) => DurationFromNanos;
check: (checks_0: SchemaAST.Check<Duration_.Duration>, ...checks: Array<SchemaAST.Check<Duration_.Duration>>) => DurationFromNanos;
rebuild: (ast: SchemaAST.Declaration) => DurationFromNanos;
make: (input: Duration_.Duration, options?: MakeOptions) => Duration_.Duration;
makeOption: (input: Duration_.Duration, options?: MakeOptions) => Option_.Option<Duration_.Duration>;
makeEffect: (input: Duration_.Duration, options?: MakeOptions) => Effect.Effect<Duration_.Duration, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
Type-level representation of
DurationFromNanos
.
Schema that decodes a non-negative bigint into a
Duration, treating the bigint as nanoseconds.
Details
Decoding:
A non-negative bigint representing nanoseconds is decoded as a Duration.
Encoding:
Finite durations are encoded as a non-negative bigint number of nanoseconds.
Encoding fails when the duration cannot be represented as nanoseconds, such as
Duration.infinity.
DurationFromNanos: DurationFromNanos = const BigInt: BigIntconst BigInt: {
Rebuild: Rebuild;
Iso: Iso;
ast: Ast;
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
annotate: (annotations: Annotations.Bottom<bigint, readonly []>) => BigInt;
annotateKey: (annotations: Annotations.Key<bigint>) => BigInt;
check: (checks_0: SchemaAST.Check<bigint>, ...checks: Array<SchemaAST.Check<bigint>>) => BigInt;
rebuild: (ast: SchemaAST.BigInt) => BigInt;
make: (input: bigint, options?: MakeOptions) => bigint;
makeOption: (input: bigint, options?: MakeOptions) => Option_.Option<bigint>;
makeEffect: (input: bigint, options?: MakeOptions) => Effect.Effect<bigint, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
Type-level representation of
BigInt
.
Schema for bigint values. Validates that the input is typeof "bigint".
When to use
Use when the input is already a bigint and the schema should validate and
preserve bigint values without parsing from another representation.
BigInt.Bottom<bigint, bigint, never, never, BigInt, BigInt, bigint, bigint, readonly [], bigint, "readonly", "required", "no-default", "readonly", "required">.check(checks_0: SchemaAST.Check<bigint>, ...checks: SchemaAST.Check<bigint>[]): BigIntcheck(const isGreaterThanOrEqualToBigInt: (
minimum: unknown,
annotations?: Annotations.Filter
) => SchemaAST.Filter<unknown>
Validates that a BigInt is greater than or equal to the specified value
(inclusive).
Details
Arbitrary:
When generating test data with fast-check, this applies a min constraint
to ensure generated BigInt values are greater than or equal to the specified
value.
isGreaterThanOrEqualToBigInt(const bigint0: bigintbigint0)).pipe(
function decodeTo<Duration, Constraint, never, never>(to: Duration, transformation: {
readonly decode: SchemaGetter.Getter<Duration_.Duration, unknown, never>;
readonly encode: SchemaGetter.Getter<unknown, Duration_.Duration, never>;
}): (from: Constraint) => decodeTo<Duration, Constraint, never, never> (+1 overload)
Creates a schema that transforms from a source schema to a target schema.
When to use
Use when decoding should change the schema's decoded type or encoded shape,
with an optional custom bidirectional transformation.
Details
Call it with the target schema to and then pipe the source schema from
into the returned function. The resulting schema decodes from
From["Encoded"] to To["Type"] and encodes from To["Type"] back to
From["Encoded"].
When no transformation is provided, SchemaTransformation.passthrough() is
used, so From["Type"] must already be compatible with To["Encoded"].
The resulting schema combines decoding and encoding services from both
schemas and any custom transformation.
Gotchas
In a custom transformation, decode maps From["Type"] to To["Encoded"]
and is used on the encoding path, while encode maps To["Encoded"] to
From["Type"] and is used on the decoding path.
Example (Transforming strings to numbers with a schema transformation)
import { Schema, SchemaGetter } from "effect"
const NumberFromString = Schema.String.pipe(
Schema.decodeTo(
Schema.Number,
{
decode: SchemaGetter.transform((s) => Number(s)),
encode: SchemaGetter.transform((n) => String(n))
}
)
)
const result = Schema.decodeUnknownSync(NumberFromString)("123")
// result: 123
decodeTo(const Duration: Durationconst Duration: {
Rebuild: Duration;
Iso: Iso;
ast: Ast;
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
annotate: (annotations: Annotations.Bottom<Duration_.Duration, readonly []>) => Duration;
annotateKey: (annotations: Annotations.Key<Duration_.Duration>) => Duration;
check: (checks_0: SchemaAST.Check<Duration_.Duration>, ...checks: Array<SchemaAST.Check<Duration_.Duration>>) => Duration;
rebuild: (ast: SchemaAST.Declaration) => Duration;
make: (input: Duration_.Duration, options?: MakeOptions) => Duration_.Duration;
makeOption: (input: Duration_.Duration, options?: MakeOptions) => Option_.Option<Duration_.Duration>;
makeEffect: (input: Duration_.Duration, options?: MakeOptions) => Effect.Effect<Duration_.Duration, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
Type-level representation of
Duration
.
Schema for Duration values.
Details
The default JSON serializer encodes Duration as a tagged object with the
duration type and value.
Example (Defining a Duration schema)
import { Duration, Schema } from "effect"
Schema.decodeUnknownSync(Schema.Duration)(Duration.seconds(5))
// => Duration(5s)
Duration, import SchemaTransformationSchemaTransformation.const durationFromNanos: Transformation<
Duration.Duration,
bigint
>
const durationFromNanos: {
_tag: 'Transformation';
decode: SchemaGetter.Getter<T, E, RD>;
encode: SchemaGetter.Getter<E, T, RE>;
flip: () => SchemaTransformation.Transformation<bigint, Duration_.Duration, never, never>;
compose: (other: SchemaTransformation.Transformation<T2, Duration_.Duration, RD2, RE2>) => SchemaTransformation.Transformation<T2, bigint, RD2, RE2>;
}
Decodes a bigint (nanoseconds) into a Duration and encodes a
Duration back to bigint nanoseconds.
When to use
Use when you need a schema transformation for nanosecond-precision timestamps
or intervals.
Details
Decoding always succeeds and creates a Duration from nanoseconds. Encoding
fails with InvalidValue if the Duration cannot be represented as a
bigint, such as Duration.infinity.
Example (Converting nanoseconds to a Duration)
import { Schema, SchemaTransformation } from "effect"
const schema = Schema.BigInt.pipe(
Schema.decodeTo(Schema.Duration, SchemaTransformation.durationFromNanos)
)
durationFromNanos)
)