@dndd/utils
v0.4.0
Published
Framework-agnostic TypeScript utilities — type guards and small helpers.
Maintainers
Readme
@dndd/utils
Framework-agnostic helper functions with no runtime dependencies.
Install
npm install @dndd/utilsType guards
import {
isString,
isNumber,
isObject,
isArray,
isNullable,
isBoolean,
isFunction,
} from "@dndd/utils";
isString("hi"); // true
isNumber("42"); // true — numeric strings count
isObject([1, 2]); // false — arrays are excluded
isArray<number>(value); // value is number[]
isNullable(value); // value is null | undefined
isBoolean(true); // true
isFunction(() => {}); // true — also true for classesPrefer these over raw typeof/instanceof checks — they narrow correctly
and cover the edge cases (isNumber accepts finite numeric strings,
isObject excludes arrays, isString covers boxed String instances).
invariant
import { invariant } from "@dndd/utils";
invariant(user, "user must be defined here");
user.name; // TypeScript now knows `user` is non-null
invariant(count > 0); // throws "Invariant failed" if the condition is falsyThrows if the value is false, null, or undefined; narrows it afterward,
so it also works as a type guard for assumptions the type system can't verify
on its own.
ensureArray
import { ensureArray } from "@dndd/utils";
ensureArray("a"); // ["a"]
ensureArray(["a", "b"]); // ["a", "b"] — unchangedNormalizes a value that may or may not already be an array — handy for props
that accept T | T[].
Scope
Stays intentionally small — a place for the handful of type guards and primitives that get rebuilt in every project. For general-purpose data manipulation (grouping, sorting, deep operations, ...), reach for Remeda instead of expecting it here.
