<S extends Constraint>(schema: S): Optic_.Iso<S["Type"], S["Iso"]>Derives an Iso optic from a schema that isomorphically converts between
the schema's Type and its Iso (intermediate / serialized form).
export function function toIso<S extends Constraint>(
schema: S
): Optic_.Iso<S["Type"], S["Iso"]>
Derives an Iso optic from a schema that isomorphically converts between
the schema's Type and its Iso (intermediate / serialized form).
toIso<function (type parameter) S in toIso<S extends Constraint>(schema: S): Optic_.Iso<S["Type"], S["Iso"]>S extends Constraint>(schema: S extends Constraintschema: function (type parameter) S in toIso<S extends Constraint>(schema: S): Optic_.Iso<S["Type"], S["Iso"]>S): import Optic_Optic_.interface Iso<in out S, in out A>A lossless, reversible conversion between types S and A.
When to use
Use when you have a pair of functions that convert back and forth without losing
information (e.g. Record ↔ entries, Celsius ↔ Fahrenheit).
- You want the strongest optic that can be composed with any other.
Details
get(s) always succeeds and returns an A.
set(a) always succeeds and returns an S.
get(set(a)) === a and set(get(s)) equals s (round-trip laws).
- Extends both
Lens
and
Prism
.
Example (Converting between Celsius and Fahrenheit)
import { Optic } from "effect"
const fahrenheit = Optic.makeIso<number, number>(
(c) => c * 9 / 5 + 32,
(f) => (f - 32) * 5 / 9
)
console.log(fahrenheit.get(100))
// Output: 212
console.log(fahrenheit.set(32))
// Output: 0
Iso<function (type parameter) S in toIso<S extends Constraint>(schema: S): Optic_.Iso<S["Type"], S["Iso"]>S["Type"], function (type parameter) S in toIso<S extends Constraint>(schema: S): Optic_.Iso<S["Type"], S["Iso"]>S["Iso"]> {
const const serializer: Codec<
S["Type"],
S["Iso"],
never,
never
>
const serializer: {
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
Rebuild: Codec<T, E, RD, RE>;
Type: T;
Iso: Iso;
ast: Ast;
annotate: (annotations: Annotations.Bottom<S['Type'], any>) => Codec<S['Type'], S['Iso'], never, never>;
annotateKey: (annotations: Annotations.Key<S['Type']>) => Codec<S['Type'], S['Iso'], never, never>;
check: (checks_0: SchemaAST.Check<S['Type']>, ...checks: Array<SchemaAST.Check<S['Type']>>) => Codec<S['Type'], S['Iso'], never, never>;
rebuild: (ast: SchemaAST.AST) => Codec<S['Type'], S['Iso'], never, never>;
make: (input: unknown, options?: MakeOptions) => S['Type'];
makeOption: (input: unknown, options?: MakeOptions) => Option_.Option<S['Type']>;
makeEffect: (input: unknown, options?: MakeOptions) => Effect.Effect<S['Type'], 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; <…;
}
serializer = function toCodecIso<S extends Constraint>(
schema: S
): Codec<S["Type"], S["Iso"]>
Derives an isomorphism codec from a schema. The encoded form is the
schema's Iso type — the intermediate representation used for round-tripping.
toCodecIso(schema: S extends Constraintschema)
return import Optic_Optic_.function makeIso<S, A>(
get: (s: S) => A,
set: (a: A) => S
): Iso<S, A>
Creates an
Iso
from a pair of conversion functions.
When to use
Use when you have two pure conversion functions that preserve all information
between S and A.
Details
The returned optic can be composed with any other optic.
Example (Wrapping and unwrapping a branded type)
import { Optic } from "effect"
type Meters = { readonly value: number }
const meters = Optic.makeIso<Meters, number>(
(m) => m.value,
(n) => ({ value: n })
)
console.log(meters.get({ value: 100 }))
// Output: 100
console.log(meters.set(42))
// Output: { value: 42 }
makeIso(import SchemaParserSchemaParser.const encodeSync: <
S extends Schema.ConstraintEncoder<unknown>
>(
schema: S,
options?: SchemaAST.ParseOptions
) => (
input: S["Type"],
options?: SchemaAST.ParseOptions
) => S["Encoded"]
Creates a synchronous encoder for input already typed as the schema's decoded
Type.
When to use
Use to encode already typed schema values synchronously when encoding failure
should throw an Error whose cause is SchemaIssue.Issue.
Details
The returned function returns the schema's Encoded value on success and throws
an Error with the SchemaIssue.Issue in its cause on encoding failure.
Gotchas
Causes that contain defects, interruptions, or asynchronous work at this
synchronous boundary throw an Error whose cause is the underlying Cause,
instead of being converted to a schema validation error.
encodeSync(const serializer: Codec<
S["Type"],
S["Iso"],
never,
never
>
const serializer: {
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
Rebuild: Codec<T, E, RD, RE>;
Type: T;
Iso: Iso;
ast: Ast;
annotate: (annotations: Annotations.Bottom<S['Type'], any>) => Codec<S['Type'], S['Iso'], never, never>;
annotateKey: (annotations: Annotations.Key<S['Type']>) => Codec<S['Type'], S['Iso'], never, never>;
check: (checks_0: SchemaAST.Check<S['Type']>, ...checks: Array<SchemaAST.Check<S['Type']>>) => Codec<S['Type'], S['Iso'], never, never>;
rebuild: (ast: SchemaAST.AST) => Codec<S['Type'], S['Iso'], never, never>;
make: (input: unknown, options?: MakeOptions) => S['Type'];
makeOption: (input: unknown, options?: MakeOptions) => Option_.Option<S['Type']>;
makeEffect: (input: unknown, options?: MakeOptions) => Effect.Effect<S['Type'], 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; <…;
}
serializer), import SchemaParserSchemaParser.const decodeSync: <
S extends Schema.ConstraintDecoder<unknown>
>(
schema: S,
options?: SchemaAST.ParseOptions
) => (
input: S["Encoded"],
options?: SchemaAST.ParseOptions
) => S["Type"]
Creates a synchronous decoder for input already typed as the schema's Encoded
type.
When to use
Use to decode values already typed as the schema's Encoded input when
decoding failure should throw an Error whose cause is SchemaIssue.Issue.
Details
The returned function returns the decoded Type on success and throws an
Error with the SchemaIssue.Issue in its cause on decoding failure.
Gotchas
Causes that contain defects, interruptions, or asynchronous work at this
synchronous boundary throw an Error whose cause is the underlying Cause,
instead of being converted to a schema validation error.
decodeSync(const serializer: Codec<
S["Type"],
S["Iso"],
never,
never
>
const serializer: {
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
Rebuild: Codec<T, E, RD, RE>;
Type: T;
Iso: Iso;
ast: Ast;
annotate: (annotations: Annotations.Bottom<S['Type'], any>) => Codec<S['Type'], S['Iso'], never, never>;
annotateKey: (annotations: Annotations.Key<S['Type']>) => Codec<S['Type'], S['Iso'], never, never>;
check: (checks_0: SchemaAST.Check<S['Type']>, ...checks: Array<SchemaAST.Check<S['Type']>>) => Codec<S['Type'], S['Iso'], never, never>;
rebuild: (ast: SchemaAST.AST) => Codec<S['Type'], S['Iso'], never, never>;
make: (input: unknown, options?: MakeOptions) => S['Type'];
makeOption: (input: unknown, options?: MakeOptions) => Option_.Option<S['Type']>;
makeEffect: (input: unknown, options?: MakeOptions) => Effect.Effect<S['Type'], 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; <…;
}
serializer))
}