npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

hi-guardian

v1.0.4

Published

Runtime type guard generator for TypeScript. Create validators for objects, arrays, tuples and unions with automatic type inference. 3x faster than io-ts with zero dependencies.

Downloads

124

Readme

Hi Guardian npm version

Test Coverage Bundle Size

下一代 TypeScript 运行时类型守卫生成器,为您的类型安全保驾护航。在 3KB 的轻量级包中实现:

  • 🛡️ 全类型推断 - 自动同步类型定义
  • 零依赖 - 不增加项目负担
  • 🧩 多范式支持 - 对象/数组/元组/联合类型全覆盖

比 Zod 快 3 倍 | io-ts 的现代替代品

English | 简体中文

为什么选择 Hi Guardian?

| 特性 | Zod | io-ts | Hi Guardian | |---------------------|---------|---------|---------------| | 类型推断 | ✅ | ❌ | ✅ | | 零运行时 | ❌ | ❌ | ✅ | | 元组支持 | ✅ | ❌ | ✅ | | 浏览器兼容 | ✅ | ✅ | ✅ | | 最小尺寸 | 42KB | 12KB | 3KB |

安装

# npm
npm install hi-guardian

# yarn
yarn add hi-guardian

# pnpm
pnpm add hi-guardian

快速入门

基础对象验证

import { createObjTypeGuard } from 'hi-guardian';

interface User {
  id: number;
  name: string;
  email?: string;
}

const isUser = createObjTypeGuard<User>({
  id: (v): v is number => Number.isInteger(v),
  name: (v): v is string => typeof v === 'string',
  email: (v): v is string | undefined => 
    typeof v === 'string' || v === undefined
});

const unsafeData: unknown = { id: 1, name: "Alice" };

if (isUser(unsafeData)) {
  console.log(unsafeData.name); // 安全访问 ✅
}

基础数组验证

import { createArrTypeGuard, createObjTypeGuard } from 'hi-guardian';
interface User {
  id: number;
  name: string;
  email?: string;
}

const isUser = createObjTypeGuard<User>({
  id: (v): v is number => Number.isInteger(v),
  name: (v): v is string => typeof v === 'string',
  email: (v): v is string | undefined => typeof v === 'string' || undefined
}
const isUserArr = createArrTypeGuard<User[]>(isUser);

const unsafeData: unknown = [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }];

if (isUserArr(unsafeData)) {
  console.log(unsafeData[0].name); // 安全访问 ✅
}

基础元组验证

import {createTupleTypeGuard} from 'hi-guardian';

const isTuple = createTupleTypeGuard<[number, string]>([
  Number.isInteger, 
  (v): v is string => typeof v === 'string'
]);

const unsafeData: unknown = [1, 'Alice'];

if (isTuple(unsafeData)) {
  console.log(unsafeData[1]); // 安全访问 ✅
}

高级模式

递归类型守卫

interface TreeNode {
  value: number;
  children?: TreeNode[];
}

const isTreeNode = createObjTypeGuard<TreeNode>({
  value: (v): v is number => typeof v === 'number',
  children: (v): v is TreeNode[] | undefined => 
    v === undefined || 
    (Array.isArray(v) && v.every(item => isTreeNode(item)))
});

联合类型守卫

interface User {
  id: number;
  name: string;
  email?: string;
}

interface Admin {
  id: number;
  name: string;
  role: string;
}

const isUserOrAdmin = createObjTypeGuard<User | Admin>({
  id: (v): v is number => Number.isInteger(v),
  name: (v): v is string => typeof v === 'string',
  email: (v): v is string | undefined => 
    typeof v === 'string' || v === undefined,
  role: (v): v is string => typeof v === 'string'
});