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

z-simple-predicates

v1.0.1

Published

Tiny, dependency-free TypeScript library of type-narrowing predicates

Readme

simple-predicates

A tiny, dependency-free TypeScript library of type-narrowing predicates. Every function it exports takes one argument and answers a yes/no question about that single unknown value.

The entire implementation is a single TypeScript file: index.ts.

Adapted from the predicates in z-validate, reduced to only the simplest cases.

Usage

import { isNonEmptyString, isNumber } from "z-simple-predicates";

isNumber("1"); // false
isNumber(NaN); // false

const parsed: unknown = JSON.parse('"hello"');
if (isNonEmptyString(parsed)) {
  parsed.toUpperCase(); // narrowed to string
}

The predicate types

type SimplePredicate = (value: unknown) => boolean;
type NarrowingPredicate<T> = (value: unknown) => value is T;

SimplePredicate is the contract every export satisfies:

  • Unary — exactly one argument, so predicates are directly usable as Array.prototype.filter and .every callbacks.
  • Total — accepts any value and does not throw, so it is safe on untrusted input without a try/catch. The one exception is a value engineered to throw when it is read — a Proxy with a throwing trap, say — which no check that has to look at the value can survive.
  • Pure — no mutation, no I/O, no dependence on anything but the argument.

NarrowingPredicate<T> is a SimplePredicate that additionally reports what a true result proves, and TypeScript treats it as a subtype: a NarrowingPredicate is accepted anywhere a SimplePredicate is expected, and SimplePredicate[] types a mixed collection of both.

Nearly every predicate here narrows. The base type exists for the ones that answer a question about a value rather than its type — isTruthy and isFalsy — where a type guard would be either unsound or vacuous.

Narrowing is lost through the base type, so annotate with NarrowingPredicate wherever the call site should narrow:

import { isString, type SimplePredicate } from "z-simple-predicates";

const mixed: unknown[] = ["a", 1, "b"];

const strings = mixed.filter(isString); // string[]

const asBase: SimplePredicate = isString;
const notStrings = mixed.filter(asBase); // unknown[]

API

Primitives

isString, isNumber, isBoolean, isBigInt, isSymbol, isNull, isUndefined

Nullishness

isNil (null or undefined), isNotNil

Truthiness

isTruthy, isFalsy — the only two that do not narrow

Objects and functions

isObject, isArray, isFunction

Number refinements

isFiniteNumber, isInteger, isSafeInteger

String refinements

isEmptyString, isNonEmptyString, isBlankString

Array refinements

isEmptyArray, isNonEmptyArray

See index.ts for the exact type and semantics of each predicate.

Semantics

  • Broken values are rejected by their own type's predicate. isNumber(NaN) is false: a value that is unusable as a number is not one. Use Number.isNaN directly when the question really is "is this NaN".
  • isFunction is typeof value === 'function', which is also true of class constructors. Those are function values but are not callable — invoking one without new throws — so a true result proves the value is a function, not that calling it will work.
  • Refinements narrow to the base type, not to a synthetic one: isNonEmptyArray narrows to readonly unknown[], not to a [unknown, ...unknown[]] tuple. Encoding "non-empty" in the type buys little and complicates every signature.

Package boundaries

The whole package is unary predicates over one value. Deliberately absent:

  • Combinators — not, either, allOf, arrayOf. Composition is the next layer up, not the simplest one.
  • Configurable predicates — isGreaterThan(n), matches(re). These are factories, not predicates.
  • Format validators — isEmail, isUrl, isUuid. These carry specs, edge cases, and opinions that go stale.
  • Schemas, error messages, coercion, and parsing. A predicate answers yes or no and says nothing about why.
  • Built-in identification — isDate, isRegExp, isError, isPromise, isMap, isSet, isPlainObject, isThenable. Answering these properly means brand checks or internal-slot probes, prototype walks, cross-realm behaviour, and a spoofing trade-off to document — none of which is simple. They are worth having, but not here.

Composition, configuration, formats, and reporting belong in a higher-level package that can depend on this one. Built-in identification is not a layer above but a heavier one alongside, and follows separately.

Contributing

See the CONTRIBUTING guide for setup and development scripts.

Packaging

Published as ESM-only with bundled type declarations, built into dist/ by npm run build. Runtime-agnostic (browsers, Node, Bun, Deno, workers), with sideEffects: false so bundlers can tree-shake unused exports.

License

MIT — unlike the rest of the surrounding monorepo, which is GPL-3.0-or-later, this package is MIT-licensed to encourage adoption.