Typezy — Runtime Type Checks & Assertions

Tiny, explicit helpers: `is*` for guards and `assert*` for throwing clear errors. Tree-shakable and ESM-first.

Install

Add Typezy to your project with your preferred package manager.

npm install --save typezy
# or
pnpm add typezy
# or
yarn add typezy
Small & focused
Minimal helpers that do one job well.
Tree-shakeable
Import single functions to keep bundles tiny.
Well-tested
Comprehensive unit tests show example usage.

Examples

Practical snippets

1) Form validation (client-side)

import { isEmail, assertNonEmptyString } from 'typezy'

function validateRegistration(data) {
  if (!isEmail(data.email)) return { ok:false, error:'Invalid email' }
  assertNonEmptyString(data.name, 'Name is required')
  return { ok:true }
}

2) API input guards (server-side)

import { isObject, assertInRange, isInteger } from 'typezy'

function handleListRequest(qs) {
  const limit = isInteger(qs.limit) ? Number(qs.limit) : 20
  assertInRange(limit, 1, 100)
  // proceed using safe `limit`
}

3) Safe parsing & validation

import { isJSON, isObject } from 'typezy'

function parsePayload(raw) {
  if (!isJSON(raw)) throw new Error('Bad payload')
  const parsed = JSON.parse(raw)
  if (!isObject(parsed)) throw new Error('Expected object')
  return parsed
}

4) Tests: assert helpers

import { assertUUID, assertEmail } from 'typezy'

test('user payload', ()=>{
  assertUUID(user.id)
  assertEmail(user.email)
})

API Highlights

Common helpers — see code for the full list and exact semantics.

FunctionTypeNotes
isString(v)GuardTrue for string primitives
isNumber(v)GuardTrue for finite numbers
isEmail(v)ValidatorLightweight RFC-like check
isUUID(v)Validatorv4 UUID format
assertString(v,msg?)AssertionThrows TypeError on failure
assertInRange(n,min,max)AssertionThrows RangeError when out of bounds