<N extends Newtype.Any>(): Optic.Iso<N, Newtype.Carrier<N>>Creates an Optic.Iso for a newtype, providing both wrapping (set) and
unwrapping (get).
When to use
Use as the primary way to construct and deconstruct newtype values.
Details
The returned iso composes with other optics via the standard Optic API.
Both directions have zero runtime cost because they are identity casts.
Example (Wrapping and unwrapping with an iso)
import { Newtype } from "effect"
interface Label extends Newtype.Newtype<"Label", string> {}
const labelIso = Newtype.makeIso<Label>()
const label: Label = labelIso.set("world")
const str: string = labelIso.get(label) // "world"export function function makeIso<
N extends Newtype.Any
>(): Optic.Iso<N, Newtype.Carrier<N>>
Creates an Optic.Iso for a newtype, providing both wrapping (set) and
unwrapping (get).
When to use
Use as the primary way to construct and deconstruct newtype values.
Details
The returned iso composes with other optics via the standard Optic API.
Both directions have zero runtime cost because they are identity casts.
Example (Wrapping and unwrapping with an iso)
import { Newtype } from "effect"
interface Label extends Newtype.Newtype<"Label", string> {}
const labelIso = Newtype.makeIso<Label>()
const label: Label = labelIso.set("world")
const str: string = labelIso.get(label) // "world"
makeIso<function (type parameter) N in makeIso<N extends Newtype.Any>(): Optic.Iso<N, Newtype.Carrier<N>>N extends Newtype.type Newtype<in out Key extends string, out Carrier>.Any = Newtype<any, unknown>A type that matches any Newtype, useful as a generic constraint:
<N extends Newtype.Any>.
When to use
Use as a generic constraint when a type parameter can be any Newtype.
Any>(): import OpticOptic.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) N in makeIso<N extends Newtype.Any>(): Optic.Iso<N, Newtype.Carrier<N>>N, Newtype.type Newtype<in out Key extends string, out Carrier>.Carrier<N extends Newtype.Any> = N extends Newtype<infer _Key extends string, infer Carrier> ? Carrier : neverExtracts the carrier (underlying) type from a newtype.
When to use
Use when you need to refer to the wrapped type in generic utilities.
Carrier<function (type parameter) N in makeIso<N extends Newtype.Any>(): Optic.Iso<N, Newtype.Carrier<N>>N>> {
return import OpticOptic.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(const value: <N extends Newtype.Any>(
newtype: N
) => Newtype.Carrier<N>
Unwraps a newtype value, returning the underlying carrier value.
When to use
Use when you need the carrier value from an existing newtype without
constructing a new newtype value at the same call site.
Details
This has zero runtime cost because it is an identity cast.
Example (Unwrapping a newtype)
import { Newtype } from "effect"
interface Label extends Newtype.Newtype<"Label", string> {}
const iso = Newtype.makeIso<Label>()
const label = iso.set("hello")
const raw: string = Newtype.value(label) // "hello"
value, import castcast)
}