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

ts-narrow

v0.27.4

Published

typescript type predicates with no dependencies

Downloads

368

Readme

ts-narrow

Typescript type predicates with no dependencies.

This library is not supposed to be a schema validator, or a object validator, so it doesn't throw any errors and will always return a boolean result. You should use this library as a quick helper to narrow from more generic types, to something that you can work with.

For example:

image

Here you're receiving an unknown object, which you can't work with on typescript. By the time the program evaluates the 'if' condition as true, constant a type will be narrow, meaning that now you can be sure that constant a is a object and has a prop b of type number.
This program will enter inside 'if' condition only if constant a matches exactly this condition, and your IDE will respect these types.

Under the hood, it uses advanced user defined types to bring the best of two worlds: a fast runtime check within a amazing developer experience.

Library methods

hasElementAt

Checks if target is an array and it has an element on a specific position.

hasElementAtOf

Checks if target is an array and it has an element on a specific position. Can be composed to check the type of the element.

hasProp

Checks if target is an object and it has a specific property.

hasPropOf

Checks if target is an object and it has a specific property. Can be composed to check the type of the property.

isArray

Checks if target is an array.

isArrayOf

Checks if target is an array. Can be composed to check the type of all elements.

isEnum

Checks if target is a typescript enum.

isEnumOf

Checks if target is a specific enum.

isEnumValueOf

Checks if target is a value of a specific enum.

isNumber

Checks if target is a number.

isObject

Checks if target is an object. Please notice that null is a valid javascript object.

isOneOf

Checks if target is one of passed arguments.

isUnionOf

Checks if target is all of passed arguments.

isString

Checks if target is a string.

isRecordOf

Checks if target is an object and it has all its properties with the same passed type.

ensure

Throws if target is null or undefined. Can receive an optional message or anything that extends Error to customize the message to be throw.

isLiteral

Checks if target is a literal with specific value.

isObjectOf

Checks if target is a object with specific shape. Can be composed to check the type of each property individually by using a NarrowFunc. It doesn't care about extra properties.

isInstanceOf

Checks if target is an instance of a class.

assert

Throws if passed condition is not true. Can receive a optional message or anything that extends Error to customize the message to be throw.

assertOf

Throws if passed condition is not true. It also narrows argument to have matched condition type. Can receive a optional message or anything that extends Error to customize the message to be throw.

isOneStringLiteralOf

Checks if string is one of strings passed in a list.

isBoolean

Checks if target is a boolean.

ensureOf

Throws if target returns false from a NarrowFunc.

isTruthy

Check if target is not null, undefined.