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 🙏

© 2025 – Pkg Stats / Ryan Hefner

ts-vchecker

v1.1.1

Published

Minimal validation library with TypeScript type inference

Downloads

4

Readme

TS VChecker

Minimal validation library with TypeScript type inference

npm install ts-vchecker

Features

This library lets you build a standard type guard declaratively.

The resulting type is inferred by TypeScript automatically, and you can give it a name if you want to reuse it in your code.

The following declarations are available (see documentation below):

| Type | Declaration | Notes | | ---------------------------- | -------------------- | ------------------------------------------------------------- | | Values: | | | | boolean | vBoolean() | | | number | vNumber() | | | string | vString() | | | null | vNull() | | | undefined | vUndefined() | | | Literal ("foo" as const) | vLiteral(...) | Treats all NaN values as equal, unlike JavaScript. | | any | vAny() | Always matches. | | never | vNever() | Never matches. | | Structures: | | | | object | vObject(...) | A schema must be provided. Extra properties are ignored. | | array | vArray(...) | A child type can be provided. | | Combinations: | | | | Union (A \| B \| ...) | vUnion(...) | A value must match one of the schemas. | | Intersection (A & B & ...) | vIntersection(...) | A value must match all of the schemas. | | Runtime logic: | | | | if with type guard | vRefine(...) | Takes a user-defined type guard to narrow down the type. | | if without type guard | vValidate(...) | Takes a user-defined validation function returning a boolean. |

Example

Declare both your type and the validation function at once:

export type MovedEvent = VCheckerType<typeof isMovedEvent>;
export const isMovedEvent = vObject({
  type: vLiteral("moved"),
  data: vUnion(
    vObject({
      parentId: vValidate(vString(), isUUID),
      index: vNumber(),
    }),
    vObject({
      parentId: vNull(),
    })
  ),
});

Use the validation function in your code like a standard type guard:

const event = req.body;

if (!isMovedEvent(event)) {
  return res.status(400).end();
}

res.status(200).send("Moving node to " + event.data.parentId);

The following types are inferred automatically by TypeScript.

// type MovedEvent = {
//   type: "moved";
//   data:
//     | {
//         parentId: string;
//         index: number;
//       }
//     | {
//         parentId: null;
//       };
// };
// const isMovedEvent: (x: unknown) => x is MovedEvent;

Documentation

Any

function vAny(): VChecker<any>
  • This checker returns true for any value

Array

function vArray(checker: VChecker<T> = vAny()): VChecker<T[]>
  • This checker returns true for empty arrays
  • This checker returns true for arrays of correctly typed values
  • This checker returns false for non-arrays
  • This checker returns false for arrays of incorrectly typed values

Boolean

function vBoolean(): VChecker<boolean>
  • This checker returns true for booleans
  • This checker returns false for non-booleans

Intersection

function vIntersection(
  schema1: VChecker<T1>,
  schema2: VChecker<T2>,
  schema3: VChecker<T3> = vAny(),
  schema4: VChecker<T4> = vAny(),
  schema5: VChecker<T5> = vAny(),
  schema6: VChecker<T6> = vAny(),
  schema7: VChecker<T7> = vAny(),
  schema8: VChecker<T8> = vAny(),
  schema9: VChecker<T9> = vAny(),
  schema10: VChecker<T10> = vAny()
): VChecker<T1 & T2 & T3 & T4 & T5 & T6 & T7 & T8 & T9 & T10>
  • This checker returns true if and only if all checks return true

Literal

function vLiteral(literal: T): VChecker<T>
  • This checker returns true for the given value
  • This checker returns true for NaN values (unlike === in JavaScript)
  • This checker returns false for not the given value

Never

function vNever(): VChecker<never>
  • This checker always returns false

Null

function vNull(): VChecker<null>
  • This checker returns true for null
  • This checker returns false for non-null values

Number

function vNumber(): VChecker<number>
  • This checker returns true for numbers
  • This checker returns false for non-numbers

Object

function vObject(
  schema: T
): VChecker<
  {
    [K in keyof T]: VCheckerType<T[K]>;
  }
>
  • This checker returns true if and only if all values in schema match
  • This checker returns true for an array if the array matches the schema (weird JavaScript quirk)
  • This checker returns true for objects with extra properties
  • This checker returns false for non-objects

Refine

function vRefine(
  checker: VChecker<T>,
  refinement: (x: T) => x is U
): VChecker<U>
  • This checker returns true if and only if both checks return true

String

function vString(): VChecker<string>
  • This checker returns true for strings
  • This checker returns false for non-strings

Undefined

function vUndefined(): VChecker<undefined>
  • This checker returns true for undefined
  • This checker returns false for non-undefined values

Union

function vUnion(
  schema1: VChecker<T1>,
  schema2: VChecker<T2>,
  schema3: VChecker<T3> = vNever(),
  schema4: VChecker<T4> = vNever(),
  schema5: VChecker<T5> = vNever(),
  schema6: VChecker<T6> = vNever(),
  schema7: VChecker<T7> = vNever(),
  schema8: VChecker<T8> = vNever(),
  schema9: VChecker<T9> = vNever(),
  schema10: VChecker<T10> = vNever()
): VChecker<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9 | T10>
  • This checker returns true for values of one of the given types
  • This checker returns false for values not of one of the given types

Validate

function vValidate(
  checker: VChecker<T>,
  validator: (x: T) => boolean
): VChecker<T>
  • This checker returns true if and only if both checks return true