{"name":"newtype-ts","version":"0.3.2","description":"Implementation of newtypes in TypeScript","main":"lib/index.js","module":"es6/index.js","typings":"lib/index.d.ts","sideEffects":false,"scripts":{"lint":"tslint src/**/*.ts test/**/*.ts","typings-checker":"typings-checker --allow-expect-error --project typings-checker/tsconfig.json typings-checker/index.ts","jest":"jest","prettier":"prettier --no-semi --single-quote --print-width 120 --parser typescript --list-different \"{src,test}/**/*.ts\"","fix-prettier":"prettier --no-semi --single-quote --print-width 120 --parser typescript --write \"{src,test}/**/*.ts\"","test":"npm run prettier && npm run lint && npm run typings-checker && npm run jest && npm run docs","clean":"rm -rf lib/*","build":"npm run clean && tsc && tsc -p tsconfig.es6.json","prepublish":"npm run build","docs":"docs-ts"},"repository":{"type":"git","url":"git+https://github.com/gcanti/newtype-ts.git"},"author":{"name":"Giulio Canti","email":"giulio.canti@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/gcanti/newtype-ts/issues"},"homepage":"https://github.com/gcanti/newtype-ts","peerDependencies":{"fp-ts":"^2.0.0","monocle-ts":"^2.0.0"},"devDependencies":{"@types/jest":"^22.2.2","@types/node":"^10.12.21","docs-ts":"0.0.1","fp-ts":"^2.0.0","jest":"^22.4.3","monocle-ts":"^2.0.0","prettier":"^1.13.5","ts-jest":"^22.4.2","ts-node":"3.3.0","tslint":"4.4.2","tslint-config-standard":"4.0.0","typescript":"^3.5.1","typings-checker":"^2.0.0"},"tags":["typescript","newtype","functional-programming"],"keywords":["typescript","newtype","functional-programming"],"jest":{"globals":{"ts-jest":{"skipBabel":true}},"collectCoverage":true,"transform":{"^.+\\.tsx?$":"ts-jest"},"testRegex":"test","moduleFileExtensions":["ts","js"],"coverageThreshold":{"global":{"branches":100,"functions":100,"lines":100,"statements":0}}},"gitHead":"7f86bf4f35f6fe76323639a939fa9860dbc9f9fb","_id":"newtype-ts@0.3.2","_npmVersion":"6.4.1","_nodeVersion":"10.1.0","_npmUser":{"name":"gcanti","email":"giulio.canti@gmail.com"},"dist":{"integrity":"sha512-1F5ff8LdXtMgcuy+t2F0q6b8VCt26YiXCw/YCSLacBBGH5N+fkFzNoRzvvP6JAjmFCb1Zcfyn0ZyJOCJIpTkKQ==","shasum":"a25db880f9c601b4ea8fc61658ffa399d60f62e2","tarball":"http://registry-npm.ywwl.com/newtype-ts/download/newtype-ts-0.3.2.tgz","fileCount":88,"unpackedSize":45802,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdGyGTCRA9TVsSAnZWagAAZ5QP/3YJrN4Ag997kWdDZ5JK\nHQAWuASMaIBBJQD89UUE5k3vQ20KmBRgan3dIvaNl5zDN+SSgLlAjVd5OpHr\nKWSctxw6esj8dTjqdQa/eo7Ko97TJd2mXqEaWdiHGtC9eyIxwkcNKwn+CLKv\nz1e3cQgK08ryofTwc+RBenTgyNbs4E8bpvAHFawsZzcHhoKfk5TmAd2WBFN1\n/g9DSk32djpUedThphTDodxTXjhPAcZI+wZC7l7pHVbGVaU2IHSOILQ7mSh6\nbIqBZtUqHaQmqpD746qkygz+rQ3JCti27RKWfqZY/3e9p0ngLs3MRujDG3D9\nGL7vdK5cvD5Y9PSPwWvsey9U/SMhuxC2fFy2xtvNNvAJpf8RTeKDsLgVCnyv\nBcy+yW/Krd81WrBvjYQ9SwntyHuObuE9A9xGvu0CHjq+nWp4a4chhivgEM8a\nxsgo1AlFO69wfuHEVUsyp2lsdvl3b7nUFWsICJRezn9KYEjUfe7LunVgCkLQ\nzfc9MRt3d7aWmTSawPfXRXsu8sHLyCSrgHVkBw5b0grIh3b8hnvBbhGFjZSI\njJgSfiKL07B+bJ76PUxLo2YKBPjBW03gAOZgcUQjhtR55schS6qosJaCCAeB\nz7x25EH/YG7keaNhqE/MOikI6FwUQWjpQ15cgBx9+vzBr9LB6dZ3bpwrb+2K\nDkFF\r\n=TcdG\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIF+HSogye2N7Arw6sAOPzw8GwjxE4R3xn5JWCyiZgIgNAiAjtxZ9qmK0ouMMMYbHKZFM9T8G/O34IzccExyEzodOHQ=="}],"size":10335,"noattachment":false,"key":"/newtype-ts/-/newtype-ts-0.3.2.tgz"},"maintainers":[{"name":"gcanti","email":"giulio.canti@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/newtype-ts_0.3.2_1562059154331_0.1711999889901934"},"_hasShrinkwrap":false,"readme":"[![build status](https://img.shields.io/travis/gcanti/newtype-ts/master.svg?style=flat-square)](https://travis-ci.org/gcanti/newtype-ts)\n![npm downloads](https://img.shields.io/npm/dm/newtype-ts.svg)\n\n# Installation\n\nTo install the stable version:\n\n```sh\nnpm i newtype-ts\n```\n\n**Note**. `newtype-ts` depends on [`fp-ts`](https://github.com/gcanti/fp-ts) and [`monocle-ts`](https://github.com/gcanti/monocle-ts), starting from `0.3.0` you must install both `fp-ts` and `monocle-ts` manually (`fp-ts` and `monocle-ts` are listed in `peerDependency`)\n\n# Motivation\n\nA common programming practice is to define a type whose representation is identical to an existing one but which has a separate identity in the type system.\n\n```ts\ntype USD = number\ntype EUR = number\n\nconst myamount: USD = 1\n\ndeclare function change(usd: USD): EUR\ndeclare function saveAmount(eur: EUR): void\n\nsaveAmount(change(myamount)) // ok\nsaveAmount(myamount) // opss... this is also ok because both EUR and USD are type alias of number!\n```\n\n# Usage\n\n## Newtypes\n\nLet's define a newtype for the EUR currency\n\n```ts\nimport { Newtype, iso } from 'newtype-ts'\n\ninterface EUR extends Newtype<{ readonly EUR: unique symbol }, number> {}\n\n// isoEUR: Iso<EUR, number>\nconst isoEUR = iso<EUR>()\n\n// myamount: EUR\nconst myamount = isoEUR.wrap(0.85)\n\n// n: number = 0.85\nconst n = isoEUR.unwrap(myamount)\n\ndeclare function saveAmount(eur: EUR): void\n\nsaveAmount(0.85) // static error: Argument of type '0.85' is not assignable to parameter of type 'EUR'\nsaveAmount(myamount) // ok\n```\n\nBy definition a \"newtype\" must have the **exact same runtime representation** as the value that it stores, e.g. a value of type `EUR` is just a `number` at runtime.\n\nFor the `Iso` type, see the [monocle-ts](https://github.com/gcanti/monocle-ts) documentation.\n\n## Refinements\n\nAn `Integer` is a refinement of `number`\n\n```ts\nimport { Newtype, prism } from 'newtype-ts'\n\ninterface Integer extends Newtype<{ readonly Integer: unique symbol }, number> {}\n\nconst isInteger = (n: number) => Number.isInteger(n)\n\n// prismInteger: Prism<number, Integer>\nconst prismInteger = prism<Integer>(isInteger)\n\n// oi: Option<Integer>\nconst oi = prismInteger.getOption(2)\n\ndeclare function f(i: Integer): void\n\nf(2) // static error: Argument of type '2' is not assignable to parameter of type 'Integer'\noi.map(f) // ok\n```\n\nFor the `Prism` type, see the [monocle-ts](https://github.com/gcanti/monocle-ts) documentation.\n\n## Builtin refinements\n\n- `Char`\n- `Integer`\n- `Negative`\n- `NegativeInteger`\n- `NonNegative`\n- `NonNegativeInteger`\n- `NonPositive`\n- `NonPositiveInteger`\n- `NonEmptyString`\n- `NonZero`\n- `NonZeroInteger`\n- `Positive`\n- `PositiveInteger`\n\n```ts\nimport { NonZero, prismNonZero } from 'newtype-ts/lib/NonZero'\n\n// a total function\nconst safeDivide = (numerator: number, denominator: NonZero): number => {\n  return numerator / prismNonZero.reverseGet(denominator)\n}\n\n// result: Option<number>\nconst result = prismNonZero.getOption(2).map(denominator => safeDivide(2, denominator))\n```\n\n# TypeScript compatibility\n\nThe stable version is tested against TypeScript 3.5.1\n\n| `newtype-ts` version | required `typescript` version | required `fp-ts` version | required `monocle-ts` version |\n| -------------------- | ----------------------------- | ------------------------ | ----------------------------- |\n| 0.3                  | 3.5.1+                        | 2.0.0-rc.6+              | 2.0.0-rc.1+                   |\n| <= 0.2.4             | 2.8+                          | 1.0.0+                   | 1.0.0+                        |\n\n# Performance\n\n```ts\nconst double = n => n * 2\nconst doubleEUR = eurIso.modify(double)\n```\n\nTest `double(2)` vs `doubleEUR(eurIso.wrap(2))`\n\nResults (`node v8.9.3`)\n\n```\ndouble x 538,301,203 ops/sec ±0.45% (87 runs sampled)\ndoubleEUR x 536,575,600 ops/sec ±0.27% (87 runs sampled)\n```\n\n# Recipes\n\n## How to lift a function\n\n```ts\nconst double = (n: number): number => n * 2\n\n// doubleEUR: (s: EUR) => EUR\nconst doubleEUR = eurIso.modify(double)\n```\n\n# Documentation\n\n- [API Reference](https://gcanti.github.io/newtype-ts)\n","publish_time":1562059154488,"_cnpm_publish_time":1562059154488,"dist-tags":{"latest":"0.3.5","next":"0.1.3-rc"}}