DurationFromStringType-level representation of DurationFromString.
export interface DurationFromString 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, String> {
readonly "Rebuild": DurationFromString
}
/**
* Schema that parses a string into a `Duration`.
*
* **Details**
*
* Decoding:
* - A `string` is decoded as a `Duration`, accepting any format that
* `Duration.fromInput` can parse.
*
* Encoding:
* - A `Duration` is encoded as a parseable `string`.
*
* @category Duration
* @since 4.0.0
*/
export const const DurationFromString: DurationFromStringconst DurationFromString: {
Rebuild: DurationFromString;
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 []>) => DurationFromString;
annotateKey: (annotations: Annotations.Key<Duration_.Duration>) => DurationFromString;
check: (checks_0: SchemaAST.Check<Duration_.Duration>, ...checks: Array<SchemaAST.Check<Duration_.Duration>>) => DurationFromString;
rebuild: (ast: SchemaAST.Declaration) => DurationFromString;
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
DurationFromString
.
Schema that parses a string into a Duration.
Details
Decoding:
- A
string is decoded as a Duration, accepting any format that
Duration.fromInput can parse.
Encoding:
- A
Duration is encoded as a parseable string.
DurationFromString: DurationFromString = const DurationString: Stringconst DurationString: {
Rebuild: Rebuild;
Iso: Iso;
ast: Ast;
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
annotate: (annotations: Annotations.Bottom<string, readonly []>) => String;
annotateKey: (annotations: Annotations.Key<string>) => String;
check: (checks_0: SchemaAST.Check<string>, ...checks: Array<SchemaAST.Check<string>>) => String;
rebuild: (ast: SchemaAST.String) => String;
make: (input: string, options?: MakeOptions) => string;
makeOption: (input: string, options?: MakeOptions) => Option_.Option<string>;
makeEffect: (input: string, options?: MakeOptions) => Effect.Effect<string, 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; <…;
}
DurationString.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 durationFromString: Transformation<
Duration.Duration,
string
>
const durationFromString: {
_tag: 'Transformation';
decode: SchemaGetter.Getter<T, E, RD>;
encode: SchemaGetter.Getter<E, T, RE>;
flip: () => SchemaTransformation.Transformation<string, Duration_.Duration, never, never>;
compose: (other: SchemaTransformation.Transformation<T2, Duration_.Duration, RD2, RE2>) => SchemaTransformation.Transformation<T2, string, RD2, RE2>;
}
Decodes a string into a Duration and encodes a Duration back to a
parseable string.
When to use
Use when you need a schema transformation to parse human-readable duration
strings from APIs, config, or user input.
Details
Decoding accepts any string that Duration.fromInput can parse, including
"Infinity" and "-Infinity". Encoding returns String(duration),
producing strings such as "2000 millis" or "10 nanos" that round-trip
through the parser.
Example (Converting a string to a Duration)
import { Schema, SchemaTransformation } from "effect"
const schema = Schema.String.pipe(
Schema.decodeTo(Schema.Duration, SchemaTransformation.durationFromString)
)
durationFromString)
)