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

@etianen/types

v0.3.4

Published

Runtime type checking of untrusted data.

Downloads

43

Readme

@etianen/types

Runtime type checking of untrusted data.

Installing

npm install '@etianen/types'

TypeScript: To take advantage of typings, be sure to set moduleResolution to "node" in your tsconfig.json.

Overview

When receiving JSON data from an untrusted source, it's tempting to just assume the data is of the expected type and shape. This can lead to confusing errors appearing deep in your code.

@etianen/types provides a mechanism to check that untrusted data is the correct shape, or throw a useful debugging error.

import {numberType, arrayOf, ValueError, fromJSON} from "@etianen/types";

const numberArrayType = arrayOf(numberType);
numberArrayType.isTypeOf([1]);  // => true
numberArrayType.isTypeOf(["foo"]);  // => false
numberArrayType.isTypeOf(true);  // => false

try {
    const trustedValue = fromJSON(untrustedString);
} catch (ex) {
    if (ex instanceof ValueError) {
        console.log(ex.toString());
    }
}

API

fromJS()

Casts value to Type, or throws a ValueError.

fromJS<T>(value: Object, type: Type<T>): T;

fromJSON()

Parses value as JSON and casts to Type, or throws a ValueError.

fromJSON<T>(value: string, type: Type<T>): T;

Type

A description of a runtime type.

Type.isTypeOf()

Checks that value is of this Type.

Type.isTypeOf<T>(value: Object): value is T;

Type.getName()

Returns the descriptive name of Type.

Type.getName(): string;

Type.equals()

Performs a value equality check on two values of this Type.

Type.equals<T>(a: T, b: T): boolean;

ValueError

Error thrown when a value is of the incorrect type.

ValueError.message

A description of the problem.

ValueError.message: string;

ValueError.stack

A stack trace to the source of the problem.

ValueError.message: string;

ValueError.value

The value that caused the error.

ValueError<T>.value: T;

ValueError.toString

A description of the problem, including the value that caused the error.

ValueError.toString(): string;

Built-in types

The library of built-in types is designed to be as strict as possible, avoiding unexpected behavior deep within your code. This means:

  • A Type does not accept null unless explicitly allowed via nullableOf().
  • A Type does not accept undefined unless explicitly allowed via optionalOf().

stringType

A Type representing string.

const stringType: Type<string>;

numberType

A Type representing number.

const numberType: Type<number>;

booleanType

A Type representing boolean.

const booleanType: Type<boolean>;

anyType

A Type representing any value that is not null or undefined.

const anyType: Type<Object>;

Typescript note: The Object type is used in place of any to avoid "poisoning" the rest of your codebase with cascading any. Use explicit type casts to convert Object to known types elsewhere in your codebase, or use intersectionOf() if multiple types are allowed.

nullableOf()

A Type modifier representing a value that may be null.

nullableOf<T>(value: Object): Type<T>;

optionalOf()

A Type modifier representing a value that may be undefined.

optionalOf<T>(value: Object): Type<T>;

intersectionOf()

A Type modifier representing a value that must be either of two Types.

intersectionOf<A, B>(typeA: Type<A>, typeB: Type<B>): Type<A | B>;

unionOf()

A Type modifier representing a value must be both of two Types.

unionOf<A, B>(typeA: Type<A>, typeB: Type<B>): Type<A & B>;

arrayOf()

A container Type representing a homogenous array of another Type.

arrayOf<T>(valueType: Type<T>): Type<Array<T>>;

objectOf()

A container Type representing a homogenous object of another Type.

objectOf<T>(valueType: Type<T>): ObjectOf<T>;

tupleOf()

A container Type representing a heterogenous array of other Types.

tupleOf<A>(types: Array<Type<A>>): Type<[A]>;
tupleOf<A, B>(types: Array<Type<A>, Type<B>>): Type<[A, B]>;
tupleOf<A, B, C>(types: Array<Type<A>, Type<B>, Type<C>>): Type<[A, B, C]>;
tupleOf<A, B, C, C>(types: Array<Type<A>, Type<B>, Type<C>, Type<D>>): Type<[A, B, C, D]>;
tupleOf<A, B, C, C, D>(types: Array<Type<A>, Type<B>, Type<C>, Type<D>, Type<E>>): Type<[A, B, C, D, E]>;
tupleOf(types: Array<Type<Object>>): Type<Array<Object>>

Typescript note: For tuples of more than 5 items, an explicit type cast will be required.

// No explicit type cast required.
const smallTupleType: Type<[string, string]> = tupleOf([stringType, stringType]);

// Explicit type cast required.
type BigTuple = [string, string, string, string, string, string];
const bigTupleType: Type<BigTuple> = tupleOf([stringType, stringType, stringType, stringType, stringType, stringType]) as Type[BigTuple];

shapeOf()

A container Type representing a heterogenous object of other Types.

shapeOf(types: ObjectOf<Type<Object>>): Type<Object>;

Typescript note: Due to lack of support in the Typescript compiler, an explicit type cast is always required.

interface MyShape {
    foo: string;
    bar: number;
}

// Explicit type cast required.
const myShapeType: Type<MyShape> = shapeOf({
    foo: stringType,
    bar: stringType,
}) as Type<MyShape>;

referenceOf()

A reference Type, representing a reference to another Type.

referenceOf<T>(getType: () => Type<T>): Type<T>;

Use this to implement circular references in Types.

const circularType = shapeOf({
    title: stringType,
    children: arrayOf(referenceOf(circularType)),
});

Build status

This project is built on every push using the Travis-CI service.

Build Status

Support and announcements

Downloads and bug tracking can be found at the main project website.

More information

This project was developed by Dave Hall. You can get the code from the project site.

Dave Hall is a freelance web developer, based in Cambridge, UK. You can usually find him on the Internet: