newtype-ts
Implementation of newtypes in TypeScript
Last updated 9 years ago by gcanti .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install newtype-ts 
SYNC missed versions from official npm registry.

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.

Current Tags

  • 0.3.5                                ...           latest (5 years ago)
  • 0.1.3-rc                                ...           next (9 years ago)

16 Versions

  • 0.3.5                                ...           5 years ago
  • 0.3.4                                ...           7 years ago
  • 0.3.3                                ...           7 years ago
  • 0.3.2                                ...           7 years ago
  • 0.3.1                                ...           7 years ago
  • 0.3.0                                ...           7 years ago
  • 0.2.4                                ...           8 years ago
  • 0.2.3                                ...           8 years ago
  • 0.2.2                                ...           8 years ago
  • 0.2.1                                ...           8 years ago
  • 0.2.0                                ...           9 years ago
  • 0.1.3-rc                                ...           9 years ago
  • 0.1.2                                ...           9 years ago
  • 0.1.1                                ...           9 years ago
  • 0.1.0                                ...           9 years ago
  • 0.0.1                                ...           9 years ago
Maintainers (1)
Downloads
Total 55
Today 6
This Week 55
This Month 55
Last Day 17
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (11)

© 2010 - cnpmjs.org x YWFE | Home | YWFE