JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 384911
  • Score
    100M100P100Q181450F
  • License MIT

Implementation of newtypes in TypeScript

Package Exports

  • newtype-ts

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (newtype-ts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Usage

import { Newtype, iso } from 'newtype-ts'

interface EUR extends Newtype<'EUR', number> {}

// eurIso: Iso<EUR, number>
const eurIso = iso<EUR>()

// eur: EUR
const eur = eurIso.wrap(2)

// n: number
export const n = eurIso.unwrap(eur)

declare function f(eur: EUR): void

f(2) // Argument of type '2' is not assignable to parameter of type 'EUR'
f(eur) // ok

For the Iso type, see monocle-ts documentation.

Lift a function

const double = (n: number): number => n * 2

// doubleEUR: (s: EUR) => EUR
const doubleEUR = eurIso.modify(double)

Operate over newtypes

import { over } from 'newtype-ts'

interface USD extends Newtype<'USD', number> {}

const USDFromEUR = (n: number): number => n * 1.18

// getter: Getter<EUR, USD>
const getter = over<EUR, USD>(USDFromEUR)

// usd: USD
const usd = getter.get(eur)

For the Getter type, see monocle-ts documentation.