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-json-check

v2.0.3

Published

Simple JSON-data typechecker for TypeScript

Downloads

748

Readme

ts-json-check

This library is a simple JSON-data typechecker for TypeScript. It will be useful when you have some arbitrary JSON-data (for example, API response) and you need to check them and cast them to the right type.

Install

npm install ts-json-check

or

yarn add ts-json-check

Usage

import {
  isObject,
  isBoolean,
  isNumber,
  isString,
  isAnyOf,
  // ...
} from "ts-json-check";

// ...

// You can't be sure that the data from the server is correct, so
// apiResponse has an 'any' type.
const apiResponse = getSomeData();

// Describe the desired shape of data using ts-json-check
// guard functions.
const isResponse = isObject({
  id: isAnyOf(isNumber, isString),
  title: isString,
  archived: isBoolean,
});

// To extract the resulting type from the guard use the GuardedType utility
// type. The APIResponse here is:
// { id: number|string, title: string, archived: boolean }
type APIResponse = GuardedType<typeof isResponse>;

if (isResponse(apiResponse)) {
  // The apiResponse have an APIResponse type here
} else {
  // apiResponse have a wrong type
}

API

This library is intended only for JSON processing, so it does not attempt to simulate all the types available in TypeScript.

Primitive guards

The following guards are corresponds to the primitive JSON types:

  • isNull
  • isNumber
  • isString
  • isBoolean

Constant guard

There is one guard that checks that the argument is a constant value (or any of constant values) of primitive JSON type: isConst.

Use it as: isConst(42) or as isConst(42, 43)

It is useful when your data can have different shape depending on value of some field (the discriminant in TS terms).

The multi-argument form of this guard (isConst(42, 43)) is equivalent to the following isAnyOf form: isAnyOf(isConst(42), isConst(43)).

'Any' guard

There is isAny guard that is always returns true and keeps it argument as any. It is useful when you don't know the exact type of your data yet and want to keep some fields untyped.

Composite guards

The composite JSON types are expressed by the following functions:

isObject

JSON object: { "foo": 42, "bar": "baz" }

Use it as: isObject({ foo: isNumber, bar: isString}). You can use any guards as the values of the argument object. The input data object can have additional keys, it is not an error.

isArray

An array of values of the same type: [1, 2, 3, 4]

Use it as: isArray(isNumber)

isTuple

An array of values of different types: [42, "baz"]. In TypeScript this corresponds to tuples.

Use it as: isTuple(isNumber, isString)

The length of the input data array must be equal to the count of the isTuple arguments. The argument list may not be empty.

Utility checkers

isAnyOf

Checks that value have one of the given types.

Use it as: isAnyOf(isNumber, isString)

The resulting type will be number | string. The isAnyOf accepts two or more checkers as arguments.

isOptional

Marks object field as optional.

Use it as: isObject({ id: isNumber, title: isOptional(isString) })

The resulting type will be { id: number, title?: string | undefined }.

Custom guards

Although this is not usually necessary, you can create your own guard functions. The type is simple: Guard<T> = (v: any) => v is T. Guard function should check argument and return true if it has the correct type of false otherwise (see the TypeScript docs).

For example let's create guard for positive numbers:

import { isNumber } from "ts-json-check";

function isPositiveNumber(v: any): v is number {
  return isNumber(v) && v > 0;
}

Note that the guarded type of isPositiveNumber is still a number. TypeScript hasn't a special type for the positive numbers.