Skip to main content
Module

x/fun/newtype.ts>Newtype

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Latest
type alias Newtype
import { type Newtype } from "https://deno.land/x/fun@v2.0.0/newtype.ts";

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.

It is important to note that while a Newtype looks like a struct, its actual representation is that of the inner type.

Examples

Example 1

import type { Newtype } from "./newtype.ts";

type Integer = Newtype<'Integer', number>;

const int = 1 as unknown as Integer;
const num = 1;

declare function addOne(n: Integer): number;

addOne(int); // This is ok!
// addOne(num); // This is not
definition: { readonly [Brand]: B; readonly [Value]: A; }