Skip to main content
Module

x/fun/newtype.ts

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Go to Latest
import * as fun from "https://deno.land/x/fun@v.2.0.0-alpha.11/newtype.ts";

Newtype presents a type level "rebranding" of an existing type.

It's basic purpose is to create a branded type from an existing type. This is much like using TypeScript type aliases, ie. type MyNumber = number. However, Newtype will prevent the existing type from being used where the Branded type is specified.

Functions

Retype an existing Eq from an inner type to a Newtype.

Retype an existing Monoid from an inner type to a Newtype.

Retype an existing Ord from an inner type to a Newtype.

Retype an existing Semigroup from an inner type to a Newtype.

If the Newtype and its underlying value are referentially transparent (meaning they can always be swapped) then you can create an instance of Iso for the Newtype for mapping back and forth.

If the Newtype and its underlying value are not referentially transparent (meaning they can always be swapped) then you can create an instance of Prism for the Newtype in order to optionally map into the Newtype given some Predicate.

Type Aliases

A type alias for Newtype<any, any> that is useful when constructing Newtype related runtime instances.

Create a branded type from an existing type. The branded type can be used anywhere the existing type can, but the existing type cannot be used where the branded one can.

Extracts the inner type value from a Newtype.