Transformation<number, string, never, never>Decodes a string into a number and encodes a number back to a
string.
When to use
Use when you need a schema transformation to parse numeric strings from APIs, form data, or URL parameters.
Details
Decoding coerces the string to a number like Number(s). Encoding coerces
the number to a string like String(n). This does not validate that the
result is finite; combine with Schema.Finite or Schema.Int for stricter
checks.
Example (Converting a string to a number)
import { Schema, SchemaTransformation } from "effect"
const schema = Schema.String.pipe(
Schema.decodeTo(Schema.Number, SchemaTransformation.numberFromString)
)export const const numberFromString: Transformation<
number,
string,
never,
never
>
const numberFromString: {
_tag: 'Transformation';
decode: SchemaGetter.Getter<T, E, RD>;
encode: SchemaGetter.Getter<E, T, RE>;
flip: () => Transformation<string, number, never, never>;
compose: (other: Transformation<T2, number, RD2, RE2>) => Transformation<T2, string, RD2, RE2>;
}
Decodes a string into a number and encodes a number back to a
string.
When to use
Use when you need a schema transformation to parse numeric strings from APIs,
form data, or URL parameters.
Details
Decoding coerces the string to a number like Number(s). Encoding coerces
the number to a string like String(n). This does not validate that the
result is finite; combine with Schema.Finite or Schema.Int for stricter
checks.
Example (Converting a string to a number)
import { Schema, SchemaTransformation } from "effect"
const schema = Schema.String.pipe(
Schema.decodeTo(Schema.Number, SchemaTransformation.numberFromString)
)
numberFromString = new constructor Transformation<unknown, unknown, never, never>(decode: SchemaGetter.Getter<unknown, unknown, never>, encode: SchemaGetter.Getter<unknown, unknown, never>): Transformation<unknown, unknown, never, never>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 SchemaGetterSchemaGetter.Number(),
import SchemaGetterSchemaGetter.String()
)