tiny-guards
TypeScript icon, indicating that this package has built-in type declarations

2.0.3 • Public • Published

tiny-guards 💂

A tiny library for advanced typescript guarding

  • 🪶 lightweight

  • 🍃 tree-shakable

  • 🧱 composable

  • 👮 type-safe

  • 🔗 zero dependencies

  • 🌚 dead simple

npm coverage

npm install tiny-guards

Usage example

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; // ✅
  }
}

Package Sidebar

Install

npm i tiny-guards

Weekly Downloads

2

Version

2.0.3

License

MIT

Unpacked Size

56.5 kB

Total Files

129

Last publish

Collaborators

  • madeofsun