DurationFromMillisType-level representation of DurationFromMillis.
export interface DurationFromMillis 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, Number> {
readonly "Rebuild": DurationFromMillis
}
/**
* Schema that decodes a non-negative (possibly infinite)
* integer into a `Duration`, treating the integer value as the duration in
* milliseconds.
*
* **Details**
*
* Decoding:
* - A non-negative (possibly infinite) integer representing milliseconds is
* decoded as a `Duration`
*
* Encoding:
* - A `Duration` is encoded to a non-negative (possibly infinite) integer
* representing milliseconds
*
* @category Duration
* @since 3.10.0
*/
export const const DurationFromMillis: DurationFromMillisconst DurationFromMillis: {
Rebuild: DurationFromMillis;
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 []>) => DurationFromMillis;
annotateKey: (annotations: Annotations.Key<Duration_.Duration>) => DurationFromMillis;
check: (checks_0: SchemaAST.Check<Duration_.Duration>, ...checks: Array<SchemaAST.Check<Duration_.Duration>>) => DurationFromMillis;
rebuild: (ast: SchemaAST.Declaration) => DurationFromMillis;
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
DurationFromMillis
.
Schema that decodes a non-negative (possibly infinite)
integer into a Duration, treating the integer value as the duration in
milliseconds.
Details
Decoding:
- A non-negative (possibly infinite) integer representing milliseconds is
decoded as a
Duration
Encoding:
- A
Duration is encoded to a non-negative (possibly infinite) integer
representing milliseconds
DurationFromMillis: DurationFromMillis = const Number: Numberconst Number: {
Rebuild: Rebuild;
Iso: Iso;
ast: Ast;
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
annotate: (annotations: Annotations.Bottom<number, readonly []>) => Number;
annotateKey: (annotations: Annotations.Key<number>) => Number;
check: (checks_0: SchemaAST.Check<number>, ...checks: Array<SchemaAST.Check<number>>) => Number;
rebuild: (ast: SchemaAST.Number) => Number;
make: (input: number, options?: MakeOptions) => number;
makeOption: (input: number, options?: MakeOptions) => Option_.Option<number>;
makeEffect: (input: number, options?: MakeOptions) => Effect.Effect<number, 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
Number
.
Schema for number values, including NaN, Infinity, and -Infinity.
Details
Default JSON serializer:
- Finite numbers are serialized as numbers.
- Non-finite values are serialized as strings (
"NaN", "Infinity", "-Infinity").
Number.Bottom<number, number, never, never, Number, Number, number, number, readonly [], number, "readonly", "required", "no-default", "readonly", "required">.check(checks_0: SchemaAST.Check<number>, ...checks: SchemaAST.Check<number>[]): Numbercheck(const isGreaterThanOrEqualTo: (
minimum: unknown,
annotations?: Annotations.Filter
) => SchemaAST.Filter<unknown>
Validates that a number is greater than or equal to the specified value
(inclusive).
Details
JSON Schema:
This check corresponds to the minimum constraint in JSON Schema.
Arbitrary:
When generating test data with fast-check, this applies a minimum constraint
to ensure generated numbers are greater than or equal to the specified value.
isGreaterThanOrEqualTo(0)).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 durationFromMillis: Transformation<
Duration.Duration,
number
>
const durationFromMillis: {
_tag: 'Transformation';
decode: SchemaGetter.Getter<T, E, RD>;
encode: SchemaGetter.Getter<E, T, RE>;
flip: () => SchemaTransformation.Transformation<number, Duration_.Duration, never, never>;
compose: (other: SchemaTransformation.Transformation<T2, Duration_.Duration, RD2, RE2>) => SchemaTransformation.Transformation<T2, number, RD2, RE2>;
}
Decodes a number of milliseconds into a Duration and encodes a Duration
back to milliseconds.
When to use
Use when you need a schema transformation to decode timeouts, delays, elapsed
intervals, or other duration values stored as millisecond counts.
Details
Decode creates a duration from the number, and encode returns the duration
length in milliseconds.
Example (Converting milliseconds to a Duration)
import { Schema, SchemaTransformation } from "effect"
const schema = Schema.Number.pipe(
Schema.decodeTo(Schema.Duration, SchemaTransformation.durationFromMillis)
)
durationFromMillis)
)