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-assurer

v0.1.2

Published

TypeScript library that provides shorthand type assertions and type guard functions for multiple types.

Downloads

1,076

Readme

type-assurer

Release Test

type-assurer is a TypeScript-first type checking library, providing compatibility with lodash's type guard functions while ensuring type safety. Designed with ESModules in mind, it allows for tree-shaking to minimize bundle sizes.

Features

  • Compatible with lodash type guard functions
  • TypeScript-first implementation with accurate type inference
  • ESModule ready for tree-shaking and bundle size optimization
  • No external dependencies
  • A collection of 7 type guard functions: a. isString - Similar to lodash's type guard functions b. assertString - Provides TypeScript's type assertion feature c. ensureString - Evaluates the argument's type and returns the value if the type guard passes, otherwise throws an exception d. fallbackString - Evaluates the first argument's type and returns the value if the type guard passes, otherwise returns the second argument's value
    • The reversed versions
    • Generator provided for custom type guards for non-primitive types

Installation

npm install type-assurer

Usage

The library provides 8 utility functions for each type guard, such as isString, isNull, etc.

And, note that fallbackNotNil can be replaced with the ?? operator. Functions that can be simplified using standard JavaScript expressions, like this example, are not targeted for implementation.

is, isNot

Functions such as is simply provide type guards that can be used in conditional branches.

import { isString } from 'type-assurer'

declare const value: unknown

if (isString(value)) {
  console.log(`This is a string: ${value}`)
} else {
  console.log('This is not a string')
}

Functions such as isNot are useful in cases that require a type guard function as an argument, such as Array.prototype.filter.

import { isNotNil } from 'type-assurer'

declare const values: string | null

const result = values.filter(isNotNil)
//    ^? string[]

assert, assertNot

Functions with names like assert assertNot are type assertion functions. If the type check does not pass, it throws a TypeError.

The second argument can contain an error message.

import { assertString } from 'type-assurer'

declare const value: unknown

assertString(value, 'Value must be a string')
// No error if value is a string, otherwise throws an error with the message "Value must be a string"

ensure, ensureNot

Functions with names like ensure ensureNot are type assertion functions, but return the same value if the type check passes. It is convenient to write type assertions on a single line.

The second argument can contain an error message.

import { ensureString } from 'type-assurer'

declare function fetchData(): Promise<string | undefined>

const value = ensureString(await fetchData(), 'Value must be a string')
//    ^? string
// No error if fetchData returns a string, otherwise throws an error with the message "Value must be a string"

fallback, fallbackNot

Functions like fallback fallbackNot are type modification functions.

They return the same value if the type check passes, otherwise they return the fallback value specified in the second argument.

import { fallbackString } from 'type-assurer'

declare function fetchData(): Promise<string | undefined>

const value = fallbackString(await fetchData(), 'default')
//    ^? string
// Returns value if it's a string, otherwise returns the fallbackValue

Contributing

Contributions are welcome! Please submit a pull request or open an issue to discuss any proposed changes or feature requests.

License

MIT