BooleanFromBitType-level representation of BooleanFromBit.
export interface BooleanFromBit 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<Boolean, interface Literals<L extends ReadonlyArray<SchemaAST.LiteralValue>>Creates a union schema from an array of literal values.
Example (Defining status codes)
import { Schema } from "effect"
const schema = Schema.Literals(["active", "inactive", "pending"])
// accepts "active", "inactive", or "pending"
Type-level representation returned by
Literals
.
Literals<readonly [0, 1]>> {
readonly "Rebuild": BooleanFromBit
}
/**
* Schema for a boolean parsed from 0 or 1.
*
* **When to use**
*
* Use when decoding data sources that represent booleans as `0 | 1` while
* keeping boolean values in the decoded model.
*
* **Details**
*
* Decoding accepts only `0 | 1`, maps `1` to `true`, and maps `0` to `false`.
* Encoding maps `true` to `1` and `false` to `0`.
*
* @see {@link Boolean} for validating values that are already booleans
* @see {@link Literals} for keeping bit literals instead of decoding them
*
* @category boolean
* @since 4.0.0
*/
export const const BooleanFromBit: BooleanFromBitconst BooleanFromBit: {
Rebuild: BooleanFromBit;
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<boolean, readonly []>) => BooleanFromBit;
annotateKey: (annotations: Annotations.Key<boolean>) => BooleanFromBit;
check: (checks_0: SchemaAST.Check<boolean>, ...checks: Array<SchemaAST.Check<boolean>>) => BooleanFromBit;
rebuild: (ast: SchemaAST.Boolean) => BooleanFromBit;
make: (input: boolean, options?: MakeOptions) => boolean;
makeOption: (input: boolean, options?: MakeOptions) => Option_.Option<boolean>;
makeEffect: (input: boolean, options?: MakeOptions) => Effect.Effect<boolean, 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
BooleanFromBit
.
Schema for a boolean parsed from 0 or 1.
When to use
Use when decoding data sources that represent booleans as 0 | 1 while
keeping boolean values in the decoded model.
Details
Decoding accepts only 0 | 1, maps 1 to true, and maps 0 to false.
Encoding maps true to 1 and false to 0.
BooleanFromBit: BooleanFromBit = function Literals<
L extends ReadonlyArray<SchemaAST.LiteralValue>
>(literals: L): Literals<L>
Creates a union schema from an array of literal values.
Example (Defining status codes)
import { Schema } from "effect"
const schema = Schema.Literals(["active", "inactive", "pending"])
// accepts "active", "inactive", or "pending"
Literals([0, 1]).pipe(
function decodeTo<Boolean, Constraint, never, never>(to: Boolean, transformation: {
readonly decode: SchemaGetter.Getter<boolean, unknown, never>;
readonly encode: SchemaGetter.Getter<unknown, boolean, never>;
}): (from: Constraint) => decodeTo<Boolean, 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 Boolean: Booleanconst Boolean: {
Rebuild: Rebuild;
Iso: Iso;
ast: Ast;
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
annotate: (annotations: Annotations.Bottom<boolean, readonly []>) => Boolean;
annotateKey: (annotations: Annotations.Key<boolean>) => Boolean;
check: (checks_0: SchemaAST.Check<boolean>, ...checks: Array<SchemaAST.Check<boolean>>) => Boolean;
rebuild: (ast: SchemaAST.Boolean) => Boolean;
make: (input: boolean, options?: MakeOptions) => boolean;
makeOption: (input: boolean, options?: MakeOptions) => Option_.Option<boolean>;
makeEffect: (input: boolean, options?: MakeOptions) => Effect.Effect<boolean, 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
Boolean
.
Schema for boolean values. Validates that the input is typeof "boolean".
When to use
Use to validate values that are already JavaScript booleans.
Boolean,
import SchemaTransformationSchemaTransformation.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: unknown) => booleandecode: (bit: unknownbit) => bit: unknownbit === 1,
encode: (input: boolean) => unknownencode: (bool: booleanbool) => bool: booleanbool ? 1 : 0
})
)
)