Package: effect
Module: Newtype
A tagged interface that wraps a carrier type under a unique key, preventing accidental interchange of structurally identical values.
When to use
Use to define a newtype as an interface extending
Newtype<"MyKey", CarrierType> when structurally identical carrier types
should remain distinct in TypeScript.
Details
The tag is compile-time only, so no runtime wrapper is allocated. Use
makeIso to create a two-way conversion, or value to unwrap.
Example (Defining a newtype)
import { Newtype } from "effect"
interface UserId extends Newtype.Newtype<"UserId", number> {}
interface OrderId extends Newtype.Newtype<"OrderId", number> {}
// UserId and OrderId are not assignable to each other
// even though both wrap `number`.
See
makeIso — create an iso to wrap and unwrapvalue — unwrap a newtype valueSignature
export interface Newtype<in out Key extends string, out Carrier> {
readonly [TypeId]: {
readonly key: Key
readonly carrier: Carrier
}
}
Since v4.0.0