# decode-ts

> TypeScript compatible value decoding.

Latest version **0.0.14** (published 2020-02-04) · 0 weekly downloads

## Install

```sh
npm install decode-ts
pnpm add decode-ts
yarn add decode-ts
bun add decode-ts
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.14 |
| Published | 2020-02-04 |
| First published | 2017-06-17 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 7.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | oliverjash |

## Links

- npm: https://www.npmjs.com/package/decode-ts
- npm.io page: https://npm.io/package/decode-ts

## Dependencies (4)

- [fp-ts](https://npm.io/package/fp-ts.md) ^2.3.1
- [io-ts](https://npm.io/package/io-ts.md) ^2.0.2
- [unionize](https://npm.io/package/unionize.md) ^1.0.0
- [io-ts-reporters](https://npm.io/package/io-ts-reporters.md) ^1.0.0

## Recent versions

- 0.0.14 (latest) — 2020-02-04
- 0.0.13 — 2018-08-11
- 0.0.12 — 2018-06-13
- 0.0.11 — 2018-03-06
- 0.0.10 — 2018-02-18
- 0.0.9 — 2018-02-17
- 0.0.8 — 2018-02-17
- 0.0.7 — 2017-11-10
- 0.0.6 — 2017-09-23
- 0.0.5 — 2017-07-28
- 0.0.4 — 2017-07-14
- 0.0.3 — 2017-07-14
- 0.0.2 — 2017-06-17
- 0.0.1 — 2017-06-17

## README

# decode-ts

TypeScript compatible value decoding.

- [io-ts] is used to perform IO validation for type safety.
- [fp-ts] is used for its `Either` type.

This is mostly a wrapper around the terrific [io-ts] to add a call to `JSON.parse`. You could achieve something similar with an io-ts type, however [there is no way to propoagate the JSON parsing errors](https://github.com/gcanti/io-ts/issues/46#issuecomment-305506173), hence the need for the wrapper.

## Installation

``` bash
yarn add decode-ts
```

## Example

``` ts
import * as t from 'io-ts';

import { jsonDecodeString, reportJsonDecodeError } from './index';

const Person = t.interface({
    id: t.string,
    age: t.number,
});

const log = (val: any) => console.log(JSON.stringify(val));

log(jsonDecodeString(Person)('foo')); // Left ParsingError
log(jsonDecodeString(Person)('{ "id": 1 }')); // Left ValidationErrors
log(jsonDecodeString(Person)('{ "id": "foo", "age": 5 }')); // Right { id: 'foo', age: 5 }

// Use the reporter for friendly error messages:

log(reportJsonDecodeError(jsonDecodeString(Person)('foo')));
// ["Unexpected token o in JSON at position 1"]
log(reportJsonDecodeError(jsonDecodeString(Person)('{ "id": 1 }')));
// ["Expecting string at id but instead got: 1.","Expecting number at age but instead got: undefined."]
```

## Development

```
yarn
yarn compile
yarn lint
```

[io-ts]: https://github.com/gcanti/io-ts
[fp-ts]: https://github.com/gcanti/fp-ts

---
_Source: https://npm.io/package/decode-ts · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
