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

@loxoz/types-check

v1.0.7

Published

A collection of handy functions to quickly check types in your code

Downloads

5

Readme

@loxoz/types-check

A collection of handy functions to quickly check types in your code.

npmGithub

✨ This library fully supports TypeScript and will infer the checked type to the variable.

👉 The main goal of this library is to reduce bundle size when checking for types (like more than two or three times) in your web app, but i also like this way of writing code with type checks.

Usage

Checking types for an API Response or json data:

import { isObj, isStr } from "@loxoz/types-check";

let data = null;

fetch("/api/data")
  .then(res => res.json())
  .then(json => {
    if (isObj(json)) data = json;
  });

// ... somewhere else ...

if (isStr(data.name)) {
  // data.name is now a string
  // ...
}

Checking arguments of a function that have multiple / optional types:

import { isStr } from "@loxoz/types-check";

function foo(bar?: string) {
  if (!isStr(bar)) bar = "default";
  // bar is a string
}

It also works using CommonJS:

const { isStr } = require("@loxoz/types-check");

API

🧪 primitives

isType

isType(o: unknown, t: string): o is JSType[T];

Exactly the same as a typeof (typeof o === t) used by other functions of this library to reduce bundle size.

You should check the other functions provided by this library first before using this one.

isStr

isStr(o: unknown): boolean

Checks if o is a string

isNum

isNum(o: unknown): boolean

Checks if o is a number

isObj

isObj(o: unknown): boolean

Checks if o is an object

This will include class instances, if you don't want that, look at isObjStrict

For typescript convenience, the inferred type is a Record

Note: this function won't include functions and null (typeof null === "object" is truthy)

isFunc

isFunc(o: unknown): boolean

Checks if o is a Function

isDef

isDef(o: unknown): boolean

Checks if o is neither null nor undefined

This function is just here for coding preferences but can actually save some bundle size if you use it a lot in your code.

Doing o != null is exactly the same (since it does that under the hood)

Also see isUndef

isUndef

isUndef(o: unknown): boolean

Checks if o is either null or undefined

This function is just here for coding preferences but can actually save some bundle size if you use it a lot in your code.

Doing o == null is exactly the same (since it does that under the hood)

Also see isDef

isDefNull

isDefNull(o: unknown): boolean

Checks if o is not undefined (but can be null)

This function is just here for coding preferences but can actually save some bundle size if you use it a lot in your code.

Doing o !== undefined is exactly the same (since it does that under the hood)

Also see isDef

other

There is also isBigInt, isBool and isSymbol, but they aren't used often so omitted from the docs

📦 objects

isArr

isArr(o: unknown): boolean

Checks if o is an Array (any[], for convenience)

isObjStrict

isObjStrict(o: unknown): boolean

Will ensure that o is an object created using {}, Object() or Object.create()

Warning: this method is much slower than isObj

This function won't include class instances, if you want that, look at isObj

The inferred type is Record<string | number | symbol, any>

🧱 helpers

isNonEmptyStr

isNonEmptyStr(o: unknown): boolean

Checks if o is a string with a least one character

isNonEmptyArr

isNonEmptyArr(o: unknown): boolean

Checks if o is an Array (any[], for convenience) with a least one element

isArrTyped

isArrTyped(o: unknown, predicate: (value: unknown, index: number, array: unknown[]) => boolean): boolean

Checks if o is an Array and all of its members satisfy the specified predicate to infer the type of that predicate
For e.g. isArrTyped(arr, isStr) will infer the type string[]

✅ assertion

assert

assert(o: unknown, predicate: (value: unknown) => boolean, message?: string): boolean

Will throw an AssertionError (which extends TypeError), with the passed message (if any) if the predicate failed (returns false)

Advices

If you need more complex type checking, for example on objects or arrays, I recommend you have a look at superstruct.