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

typeofit

v0.0.7

Published

A tiny composable type guard utility library

Downloads

150

Readme

TypeScript Type Guards Library

This is a TypeScript library that provides a set of utility functions for type guarding. Type guarding allows you to perform runtime checks on values to determine if they satisfy certain conditions. The library offers various functions to check and validate types, create literal type guards, validate arrays and tuples, and more.

Build Status Build Size Version

npm install typeofit

API Reference

Installation

Install the library using npm or yarn:

npm install my-type-guards-library

Usage

import { tp } from 'typeofit'

const unknown: unknown;

if (tp.string(unknown)) // string

if (tp.number(unknown)) // number

if (tp.array()(unknown)) // unknown[]

if (tp.array(tp.string)(unknown)) // string[]

if (tp.record()(unknown)) // Record<string, any>

if (tp.record(tp.literal('foo'), tp.number)(unknown)) // { foo: number }

if (tp.tuple(tp.literal('foo'), tp.literal(10))(unknown)) // ['foo', 10]

if (tp.struct({ foo: tp.literal('bar') })(unknown)) // { foo: 'bar' }

if (tp.or(tp.literal('foo'), tp.literal('bar'))(unknown)) // 'foo' | 'bar'

const items: (number | null)[]
const defined = items.filter(tp.defined) // number[]

API Reference

string

A type guard function that checks if a value is of type string.

object

A type guard function that checks if a value is of type object.

number

A type guard function that checks if a value is of type number.

undef

A type guard function that checks if a value is undefined.

func

A type guard function that checks if a value is of type Function.

nul

A type guard function that checks if a value is null.

literal<T extends any>(of: T)

The literal function creates a type guard that checks if a value is equal to a specific literal value.

array<T = any>(of?: Guard<T>)

The array function checks if a value is an array and optionally checks if all elements of the array match a given type guard.

tuple<T extends Guard<any>[]>(...of: T)

The tuple function checks if a value is an array with elements that match the specified guards.

record<K extends string = string, V = any>(key?: Guard<K>, value?: Guard<V>)

The record function checks if a given value is a valid record object, with optional key and value guards.

struct<T extends Record<any, Guard<any>>>(of: T, strict: boolean = true)

The struct function is a type guard that checks if an object matches a given structure defined by a record of guards.

defined<T>(x: T | undefined | null): x is T

The defined function checks if a value is defined (not undefined or null) and returns a boolean.

or<T extends Guard<any>[]>(...of: T)

The or function allows you to check if a value matches any of the provided guards.

is<T extends V, V = unknown>(cb: (x: V) => boolean): Guard<T, V>

The is function is a TypeScript type guard that checks if a value satisfies a given condition. It takes a callback function cb that takes a value of type V and returns a boolean value. The callback function is used to define the condition for type guarding.

Guard<T extends V, V = unknown>

A type alias representing a TypeScript type guard function. It takes a value of type V and returns a boolean indicating whether the value is of type T.

TypeOfGuard<G>

A conditional type that extracts the guarded type from a Guard function.

License

MIT © Dan Beaven