A tiny library for advanced typescript guarding
-
🪶 lightweight
-
🍃 tree-shakable
-
🧱 composable
-
👮 type-safe
-
🔗 zero dependencies
-
🌚 dead simple
npm install tiny-guards
import {
gt,
isNumber,
isString,
maxLen,
oneOf,
optional,
refine,
shape,
Guard,
} from "tiny-guards";
const isNaturalNumber = refine(isNumber, Number.isSafeInteger, gt(0));
const isShortString = refine(isString, maxLen(16));
const isAccountType = oneOf(["reader", "publisher", "moderator"]);
const isUser = shape({
id: isNaturalNumber,
username: isShortString,
accountType: isAccountType,
firstName: optional(isString),
lastName: optional(isString),
});
type User = Guard.Infer<typeof isUser>;
function doSomething(v: unknown) {
if (isUser(v)) {
v.id; // ✅
v.username; // ✅
}
}