Transformation<Duration.Duration, number, never, never>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)
)export const const durationFromMillis: Transformation<
Duration.Duration,
number
>
const durationFromMillis: {
_tag: 'Transformation';
decode: SchemaGetter.Getter<T, E, RD>;
encode: SchemaGetter.Getter<E, T, RE>;
flip: () => Transformation<number, Duration.Duration, never, never>;
compose: (other: Transformation<T2, Duration.Duration, RD2, RE2>) => 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: class Transformation<in out T, in out E, RD = never, RE = never>class Transformation {
_tag: 'Transformation';
decode: SchemaGetter.Getter<T, E, RD>;
encode: SchemaGetter.Getter<E, T, RE>;
flip: () => Transformation<E, T, RE, RD>;
compose: <T2, RD2, RE2>(other: Transformation<T2, T, RD2, RE2>) => Transformation<T2, E, RD | RD2, RE | RE2>;
}
Represents a bidirectional transformation between a decoded type T and an encoded
type E, built from a pair of Getters.
When to use
Use when you need a schema transformation that defines how a schema converts
between two representations.
- You want to compose multiple transformations into a pipeline.
- You want to flip a transformation to swap decode/encode.
Details
This is the primary building block for Schema.decodeTo, Schema.encodeTo,
Schema.decode, Schema.encode, and Schema.link. Each direction is a
SchemaGetter.Getter that handles optionality, failure, and Effect services.
- Immutable —
flip() and compose() return new instances.
flip() swaps the decode and encode getters.
compose(other) chains: this.decode then other.decode for decoding,
other.encode then this.encode for encoding.
Example (Composing two transformations)
import { SchemaTransformation } from "effect"
const trimAndLower = SchemaTransformation.trim().compose(
SchemaTransformation.toLowerCase()
)
// decode: trim then lowercase
// encode: passthrough (both directions)
Transformation<import DurationDuration.type Duration.Duration = /*unresolved*/ anyDuration, number> = function transform<T, E>(options: {
readonly decode: (input: E) => T
readonly encode: (input: T) => E
}): Transformation<T, E>
Creates a Transformation from pure (sync, infallible) decode and encode
functions.
When to use
Use when you need an infallible schema transformation that does not require
Effect services.
Details
- Each function receives the input and returns the output directly.
- Skips
None inputs (missing keys) — functions are only called on present values.
- Does not allocate Effects internally; uses optimized sync path.
Example (Converting between cents and dollars)
import { Schema, SchemaTransformation } from "effect"
const CentsFromDollars = Schema.Number.pipe(
Schema.decodeTo(
Schema.Number,
SchemaTransformation.transform({
decode: (dollars) => dollars * 100,
encode: (cents) => cents / 100
})
)
)
transform({
decode: (input: number) => anydecode: (i: numberi) => import DurationDuration.millis(i: numberi),
encode: (input: any) => numberencode: (a: Duration.Duration(parameter) a: {
value: DurationValue;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
a) => import DurationDuration.toMillis(a: Duration.Duration(parameter) a: {
value: DurationValue;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
a)
})