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.
import * as JSON from 'ts-typed-json';
interface JSON.Object extends Record<string, JSON.Value> {}
interface JSON.Array extends Array<JSON.Value> {}
type JSON.Value = null | boolean | number | string | JSON.Object | JSON.Array;
Tests if a JSON value is null
.
Tests if a JSON value is a boolean.
Tests if a JSON value is a number.
Tests if a JSON value is a string.
Tests if a JSON value is a JSON object.
Tests if a JSON value is a JSON array.
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.
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.
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.
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.
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.
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.
Parses a source string as a JSON value.
Serializes a JSON value to a string.
Synchronously loads a JSON value from the filesystem.
Asynchronously loads a JSON value from the filesystem.