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 🙏

© 2024 – Pkg Stats / Ryan Hefner

type-guards-ts

v1.0.3

Published

A library for TypeScript type guards

Downloads

7

Readme

Type Guards TS

type-guards-ts is a TypeScript library providing a collection of type guard functions to help you perform runtime type checking and type narrowing. This library includes functions to check if a value is of a certain type (e.g., isBoolean) as well as functions to check if a value is not of a certain type (e.g., isNotBoolean).

Index

Installation

To install the package, run the following command:

npm install type-guards-ts

How to Use

To use the type guards provided by type-guards-ts, you can import the required functions from the package and use them in your TypeScript code. Here’s a quick example:

import { isBoolean } from "type-guards-ts";

const value: unknown = true;

if (isBoolean(value)) {
    // TypeScript now knows that `value` is a boolean
    console.log("The value is a boolean.");
} else {
    console.log("The value is not a boolean.");
}

Available Type Guards

  • isBigInt
  • isBoolean
  • isFunction
  • isNull
  • isNumber
  • isObject
  • isString
  • isSymbol
  • isUndefined
  • isArray
  • isNotBigInt
  • isNotBoolean
  • isNotFunction
  • isNotNull
  • isNotNumber
  • isNotObject
  • isNotString
  • isNotSymbol
  • isNotUndefined
  • isNotArray

How to Use Each Type Guard

isBigInt

Checks if a value is a BigInt.

import { isBigInt } from "type-guards-ts";

const value: unknown = BigInt(123);

if (isBigInt(value)) {
    console.log("The value is a BigInt.");
} else {
    console.log("The value is not a BigInt.");
}

isBoolean

Checks if a value is a boolean.

import { isBoolean } from "type-guards-ts";

const value: unknown = true;

if (isBoolean(value)) {
    console.log("The value is a boolean.");
} else {
    console.log("The value is not a boolean.");
}

isFunction

Checks if a value is a function.

import { isFunction } from "type-guards-ts";

const value: unknown = () => {};

if (isFunction(value)) {
    console.log("The value is a function.");
} else {
    console.log("The value is not a function.");
}

isNull

Checks if a value is null.

import { isNull } from "type-guards-ts";

const value: unknown = null;

if (isNull(value)) {
    console.log("The value is null.");
} else {
    console.log("The value is not null.");
}

isNumber

Checks if a value is a number.

import { isNumber } from "type-guards-ts";

const value: unknown = 42;

if (isNumber(value)) {
    console.log("The value is a number.");
} else {
    console.log("The value is not a number.");
}

isObject

Checks if a value is an object.

import { isObject } from "type-guards-ts";

const value: unknown = { key: "value" };

if (isObject(value)) {
    console.log("The value is an object.");
} else {
    console.log("The value is not an object.");
}

isString

Checks if a value is a string.

import { isString } from "type-guards-ts";

const value: unknown = "Hello, world!";

if (isString(value)) {
    console.log("The value is a string.");
} else {
    console.log("The value is not a string.");
}

isSymbol

Checks if a value is a symbol.

import { isSymbol } from "type-guards-ts";

const value: unknown = Symbol("symbol");

if (isSymbol(value)) {
    console.log("The value is a symbol.");
} else {
    console.log("The value is not a symbol.");
}

isUndefined

Checks if a value is undefined.

import { isUndefined } from "type-guards-ts";

const value: unknown = undefined;

if (isUndefined(value)) {
    console.log("The value is undefined.");
} else {
    console.log("The value is not undefined.");
}

isArray

Checks if a value is an array.

import { isArray } from "type-guards-ts";

const value: unknown = [];

if (isArray(value)) {
    console.log("The value is an array.");
} else {
    console.log("The value is not array.");
}

isNotBigInt

Checks if a value is not a BigInt.

import { isNotBigInt } from "type-guards-ts";

const value: unknown = 123;

if (isNotBigInt(value)) {
    console.log("The value is not a BigInt.");
} else {
    console.log("The value is unknown.");
}

isNotBoolean

Checks if a value is not a boolean.

import { isNotBoolean } from "type-guards-ts";

const value: unknown = "true";

if (isNotBoolean(value)) {
    console.log("The value is not a boolean.");
} else {
    console.log("The value is unknown.");
}

isNotFunction

Checks if a value is not a function.

import { isNotFunction } from "type-guards-ts";

const value: unknown = "function";

if (isNotFunction(value)) {
    console.log("The value is not a function.");
} else {
    console.log("The value is unknown.");
}

isNotNull

Checks if a value is not null.

import { isNotNull } from "type-guards-ts";

const value: unknown = "not null";

if (isNotNull(value)) {
    console.log("The value is not null.");
} else {
    console.log("The value is unknown.");
}

isNotNumber

Checks if a value is not a number.

import { isNotNumber } from "type-guards-ts";

const value: unknown = "42";

if (isNotNumber(value)) {
    console.log("The value is not a number.");
} else {
    console.log("The value is unknown.");
}

isNotObject

Checks if a value is not an object.

import { isNotObject } from "type-guards-ts";

const value: unknown = "object";

if (isNotObject(value)) {
    console.log("The value is not an object.");
} else {
    console.log("The value is unknown.");
}

isNotString

Checks if a value is not a string.

import { isNotString } from "type-guards-ts";

const value: unknown = 123;

if (isNotString(value)) {
    console.log("The value is not a string.");
} else {
    console.log("The value is unknown.");
}

isNotSymbol

Checks if a value is not a symbol.

import { isNotSymbol } from "type-guards-ts";

const value: unknown = "symbol";

if (isNotSymbol(value)) {
    console.log("The value is not a symbol.");
} else {
    console.log("The value is unknown.");
}

isNotUndefined

Checks if a value is not undefined.

import { isNotUndefined } from "type-guards-ts";

const value: unknown = "defined";

if (isNotUndefined(value)) {
    console.log("The value is not undefined.");
} else {
    console.log("The value is unknown.");
}

isNotArray

Checks if a value is not an array.

import { isNotArray } from "type-guards-ts";

const value: unknown = "not array";

if (isNotArray(value)) {
    console.log("The value is not an array.");
} else {
    console.log("The value is not unknown.");
}

Future Releases

Currently, this package focuses on type guards for primitive types. However, we're actively working on expanding the library to include type guards for non-primitive types. Stay tuned for future releases where you'll find new and enhanced functionalities!

Keep your code safe and type-safe with type-guards-ts! 🚀