1.0.2 • Published 8 months ago

@imhonglu/type-guard v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

@imhonglu/type-guard

English | 한국어

Introduction

  • A library providing zero dependencies type guard utilities.
  • Provides a chaining API inspired by Jest's matcher pattern.
  • Built on Proxy to minimize overhead and ensure type safety.

demo-1

Table of Contents

Installation

npm install @imhonglu/type-guard

Usage

Type guards created using the composeGuards function can be negated using the not key.

For detailed usage examples, please refer to the API Reference.

import { composeGuards } from '@imhonglu/type-guard';

// Composing type guards
const has = composeGuards({
	length: (value: unknown): value is object & { length: number } =>
		typeof value === "object" && value !== null && "length" in value,
});
// {
//   length: [Function];
//   not: {
//     length: [Function];
//   };
// }

let value: unknown[] | number | undefined;

if (has.length(value)) { ... } // value is unknown[]
if (has.not.length(value)) { ... } // value is number | undefined

API Reference