Skip to content

Newtype

Creates compile-time-only wrappers around existing value types.

A newtype lets TypeScript distinguish values with the same runtime shape, such as two different ids that are both strings. The tag exists only in the type system, so wrapping does not allocate a runtime object. This module includes the base Newtype interface, wrapping and unwrapping helpers, optics, and helpers for reusing carrier instances such as Equivalence, Order, Combiner, and Reducer.

8 exports Added in v4.0.0 Source

Constructors

makeCombiner

Added in v4.0.0 Source

Lifts a Combiner for the carrier type into a Combiner for the newtype.

When to use

Use when you need to combine newtype-wrapped values with the carrier's combining logic, without manually unwrapping.

Details

The returned combiner delegates to the provided carrier combiner.

See

Signature

declare const makeCombiner: <N extends Newtype.Any>(
  combiner: Combiner.Combiner<Newtype.Carrier<N>>,
) => Combiner.Combiner<N>;

Lifts an Equivalence for the carrier type into an Equivalence for the newtype.

When to use

Use when you need equality for newtype-wrapped values to behave like equality for the wrapped carrier value, without manually unwrapping.

Details

The returned equivalence delegates to the provided carrier equivalence and has zero runtime cost beyond the underlying equivalence check.

See

Signature

declare const makeEquivalence: <N extends Newtype.Any>(
  equivalence: Equivalence.Equivalence<Newtype.Carrier<N>>,
) => Equivalence.Equivalence<N>;

makeIso

Added in v4.0.0 Source

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.

See

Signature

declare function makeIso<N extends Any>(): Iso<N, Carrier<N>>;

makeOrder

Added in v4.0.0 Source

Lifts an Order for the carrier type into an Order for the newtype.

When to use

Use when you need to sort newtype-wrapped values according to the ordering of the wrapped carrier value, without manually unwrapping.

Details

The returned order delegates to the provided carrier order.

See

Signature

declare const makeOrder: <N extends Newtype.Any>(
  order: Order.Order<Newtype.Carrier<N>>,
) => Order.Order<N>;

makeReducer

Added in v4.0.0 Source

Lifts a Reducer for the carrier type into a Reducer for the newtype.

When to use

Use when you need to reduce a collection of newtype-wrapped values with the carrier's reducer, without manually unwrapping.

Details

The returned reducer delegates to the provided carrier reducer.

See

Signature

declare const makeReducer: <N extends Newtype.Any>(
  reducer: Reducer.Reducer<Newtype.Carrier<N>>,
) => Reducer.Reducer<N>;

Getters

value

Added in v4.0.0 Source

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.

See

  • makeIso — two-way conversion (wrap and unwrap)

Signature

declare const value: <N extends Newtype.Any>(newtype: N) => Newtype.Carrier<N>;

Models

Newtype interface

Added in v4.0.0 Source

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.

See

  • makeIso — create an iso to wrap and unwrap
  • value — unwrap a newtype value

Signature

interface Newtype<in out Key extends string, out Carrier> {
  readonly "~effect/Newtype": {
    readonly carrier: Carrier;
    readonly key: Key;
  };
}

Other

Newtype

Added in v4.0.0 Source

Namespace containing type-level helpers for Newtype values, including constraints and utilities for extracting a newtype's key and carrier type.

When to use

Use to access generic constraints and type-level utilities for Newtype values.