npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-typed-json

v0.3.2

Published

JSON types and utilities for TypeScript.

Downloads

27,974

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

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

Types

interface JSON.Object

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

interface JSON.Array

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

type JSON.Value

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.