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 🙏

© 2026 – Pkg Stats / Ryan Hefner

with-type-checkers

v1.0.6

Published

A JavaScript/TypeScript utility for adding runtime type checking and assertions to classes

Downloads

148

Readme

with-type-checkers

A JavaScript/TypeScript utility for adding runtime type checking and assertions to classes.

Installation

npm install with-type-checkers

Usage

Basic Class Mixin

import withTypeCheckers from 'with-type-checkers';

class MyClass extends withTypeCheckers() {
  setName(name) {
    this.assert.is.string(name, 'name');
    this.name = name;
  }
  
  setAge(age) {
    this.assert.is.number(age, 'age');
    this.age = age;
  }
}

const instance = new MyClass();
instance.setName("John"); // OK
instance.setName(123); // Throws: "MyClass name expected string but got 123"

Extending Existing Classes

class BaseUser {
  constructor(id) {
    this.id = id;
  }
}

class User extends withTypeCheckers(BaseUser) {
  setEmail(email) {
    this.assert.is.string(email, 'email');
    this.check.is.notEmptyString(email, 'email'); // Warning instead of error
    this.email = email;
  }
}

Configuration Options

class MyClass extends withTypeCheckers({
  classPrefix: 'CustomClass',
  instancePrefix: function() { return `#${this.id}`; }
}) {
  constructor(id) {
    super();
    this.id = id;
  }
  
  validate(data) {
    this.assert.is.object(data, 'data'); 
    // Error: "CustomClass #123 data expected object but got null"
  }
}

API

Type Checkers

Available on both assert.is and check.is:

| Checker | Description | |---------|-------------| | object | Non-null object (excludes arrays) | | plainObject | Plain object literal | | string | String primitive | | number | Number primitive | | boolean | Boolean primitive | | function | Function | | array | Array | | null | Null value | | undefined | Undefined value | | nullish | Null or undefined | | symbol | Symbol primitive | | bigint | BigInt primitive | | date | Valid Date object | | regexp | RegExp object | | error | Error instance | | promise | Promise or thenable | | set | Set instance | | map | Map instance | | weakset | WeakSet instance | | weakmap | WeakMap instance | | iterable | Object with Symbol.iterator | | numeric | Parseable number (string or number) | | emptyString | Empty string | | notEmptyString | Non-empty string | | emptyArray | Empty array | | notEmptyArray | Non-empty array | | falsy | Falsy value | | truthy | Truthy value | | primitive | Primitive value (not object/function) | | asyncFunction | Async function | | syncFunction | Synchronous function |

Methods

assert.is.{type}(value, description)

Throws an error if the type check fails.

assert.is.not.{type}(value, description)

Throws an error if the type check passes.

check.is.{type}(value, description)

Logs a warning if the type check fails.

check.is.not.{type}(value, description)

Logs a warning if the type check passes.

is(type, value)

Returns boolean. Supports union types with |:

this.is('string|number', value); // true if string OR number

Utility Methods

  • log(message) - Console log with prefix
  • warn(message) - Console warning with prefix
  • error(message) - Console error with prefix
  • debug(message) - Console debug with prefix
  • throw(message) - Throw error with prefix

Custom Type Checkers

import { createWithTypeCheckers } from 'with-type-checkers';

const withCustomCheckers = createWithTypeCheckers({
  email: v => typeof v === 'string' && /\S+@\S+\.\S+/.test(v),
  positiveInteger: v => Number.isInteger(v) && v > 0
});

class User extends withCustomCheckers() {
  setEmail(email) {
    this.assert.is.email(email, 'email');
  }
  
  setAge(age) {
    this.assert.is.positiveInteger(age, 'age');
  }
}

Error Messages

Error messages follow the format: {prefix} {description} expected {type} but got {value}

Example: "MyClass #123 email expected string but got 456"

License

MIT