# ts-typed-json

> JSON types and utilities for TypeScript.

Latest version **0.3.2** (published 2020-08-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-typed-json
pnpm add ts-typed-json
yarn add ts-typed-json
bun add ts-typed-json
```

## 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.3.2 |
| Published | 2020-08-01 |
| First published | 2017-04-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 17.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Dave Herman |
| Maintainers | dherman |

## Links

- npm: https://www.npmjs.com/package/ts-typed-json
- Repository: https://github.com/dherman/ts-typed-json
- Homepage: https://github.com/dherman/ts-typed-json#readme
- Issues: https://github.com/dherman/ts-typed-json/issues
- npm.io page: https://npm.io/package/ts-typed-json

## Recent versions

- 0.3.2 (latest) — 2020-08-01
- 0.3.1 — 2020-07-26
- 0.3.0 — 2020-07-26
- 0.2.2 — 2017-04-28
- 0.2.1 — 2017-04-28
- 0.2.0 — 2017-04-28
- 0.1.0 — 2017-04-26

## README

# Typed JSON

This library is a set of type definitions and utilities
for dealing with JSON data in a type-safe way with TypeScript.

The most important type definitions are `JSON.Value`, `JSON.Object`,
and `JSON.Array`, which correspond respectively to JSON values,
objects, and arrays, as the names suggest.

The library also exports safely-typed versions of the standard `JSON.parse()` and `JSON.stringify()` functions.

# Basic Usage

```ts
import * as JSON from 'ts-typed-json';
```

# Types

## interface JSON.Object

```ts
interface JSON.Object extends Record<string, JSON.Value> {}
```

## interface JSON.Array

```ts
interface JSON.Array extends Array<JSON.Value> {}
```

## type JSON.Value

```ts
type JSON.Value = null | boolean | number | string | JSON.Object | JSON.Array;
```

# Type Testing API

## function JSON.isNull(x: JSON.Value): x is null

Tests if a JSON value is `null`.

## function JSON.isBoolean(x: JSON.Value): x is boolean

Tests if a JSON value is a boolean.

## function JSON.isNumber(x: JSON.Value): x is number

Tests if a JSON value is a number.

## function JSON.isString(x: JSON.Value): x is string

Tests if a JSON value is a string.

## function JSON.isObject(x: JSON.Value): x is JSON.Object

Tests if a JSON value is a JSON object.

## function JSON.isArray(x: JSON.Value): x is JSON.Array

Tests if a JSON value is a JSON array.

# Type Cast API

## function JSON.asNull(x: JSON.Value, prefix?: string): null

Casts a JSON value to `null`, throwing a `TypeError` if it fails.

The optional `prefix` argument allows callers to provide a contextual
string describing the value that is being tested, for the sake of
generating useful error messages in the case of a type error.

## function JSON.asBoolean(x: JSON.Value, prefix?: string): boolean

Casts a JSON value to a boolean, throwing a `TypeError` if it fails.

The optional `prefix` argument allows callers to provide a contextual
string describing the value that is being tested, for the sake of
generating useful error messages in the case of a type error.

## function JSON.asNumber(x: JSON.Value, prefix?: string): number

Casts a JSON value to a number, throwing a `TypeError` if it fails.

The optional `prefix` argument allows callers to provide a contextual
string describing the value that is being tested, for the sake of
generating useful error messages in the case of a type error.

## function JSON.asString(x: JSON.Value, prefix?: string): string

Casts a JSON value to a string, throwing a `TypeError` if it fails.

The optional `prefix` argument allows callers to provide a contextual
string describing the value that is being tested, for the sake of
generating useful error messages in the case of a type error.

## function JSON.asObject(x: JSON.Value, prefix?: string): JSON.Object

Casts a JSON value to a JSON object, throwing a `TypeError` if it fails.

The optional `prefix` argument allows callers to provide a contextual
string describing the value that is being tested, for the sake of
generating useful error messages in the case of a type error.

## function JSON.asArray(x: JSON.Value, prefix?: string): JSON.Array

Casts a JSON value to a JSON array, throwing a `TypeError` if it fails.

The optional `prefix` argument allows callers to provide a contextual
string describing the value that is being tested, for the sake of
generating useful error messages in the case of a type error.


# Serialization and Deserialization API

## function parse(source: string): JSON.Value

Parses a source string as a JSON value.

## function stringify(value: JSON.Value): string

Serializes a JSON value to a string.

## function loadSync(path: string, encoding: BufferEncoding = 'utf8'): JSON.Value

Synchronously loads a JSON value from the filesystem.

## function load(path: string, encoding: BufferEncoding = 'utf8'): Promise<JSON.Value>

Asynchronously loads a JSON value from the filesystem.

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