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.filterand.everycallbacks. - 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 — aProxywith 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)isfalse: a value that is unusable as a number is not one. UseNumber.isNaNdirectly when the question really is "is thisNaN". isFunctionistypeof value === 'function', which is also true of class constructors. Those are function values but are not callable — invoking one withoutnewthrows — so atrueresult proves the value is a function, not that calling it will work.- Refinements narrow to the base type, not to a synthetic one:
isNonEmptyArraynarrows toreadonly 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.
