New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

type-guardians

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

type-guardians

A lightweight, zero-dependency TypeScript library providing type guards and assertions for safer and more readable code

latest
Source
npmnpm
Version
1.2.5
Version published
Maintainers
1
Created
Source

Type Guardian

Type Guardian is a lightweight, zero-dependency TypeScript library that provides a collection of type guards and assertions to help you write safer and more readable code.

Installation

Install the package using your favorite package manager:

npm install type-guardians
# or
yarn add type-guardians
# or
pnpm add type-guardians

Usage

Type Guardian offers two types of functions for each data type: is functions for type guarding and assert functions for type assertions.

is functions

is functions are type guards that return a boolean value indicating whether the input value is of the expected type. You can use them in if statements to narrow down the type of a variable.

import { isString } from "type-guardians/string";

function greet(name: unknown) {
  if (isString(name)) {
    // name is now of type string
    console.log(`Hello, ${name}!`);
  } else {
    console.log("Hello, guest!");
  }
}

assert functions

assert functions are assertions that throw a TypeError if the input value is not of the expected type. You can use them to ensure that a variable has a certain type before using it. You can also provide a custom error message as an optional second parameter.

import { assertNumber } from "type-guardians/number";

function double(value: unknown) {
  assertNumber(value);
  // value is now of type number
  return value * 2;
}

API

String

  • isString(value: unknown): value is string
  • assertString(value: unknown, message?: string): asserts value is string

Number

  • isNumber(value: unknown): value is number
  • assertNumber(value: unknown, message?: string): asserts value is number

Error

  • isError(value: unknown): value is Error
  • assertError(value: unknown, message?: string): asserts value is Error

Keywords

typescript

FAQs

Package last updated on 07 Jan 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts