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

unnarrowed

v0.1.0

Published

A type-only TypeScript library for detecting unnarrowed types and asserting narrowing of generic types.

Downloads

2

Readme

narrowable

A type-only TypeScript library for detecting unnarrowed types and asserting narrowing of generic types.

What can I do with this?

Types

Unnarrowed

A type that has not been narrowed yet.

type Unnarrowed<T>

Usage:

interface AssociatedTypes {
    DateType: Unnarrowed<Date | number | string>
}

NarrowedFrom

The narrowed form of an Unnarrowed type.

If used on the right-hand side of an extends statement such as MyClass<U extends NarrowedFrom<T>> where T is Unnarrowed, it will cause an error when U = T.

type NarrowedFrom<T>

Behavior:

  • If T is Unnarrowed<U>, returns U.
  • If T is an object with Unnarrowed<U> properties, the Unnarrowed is stripped from them.
  • Otherwise, returns an error type.

Usage:

type DateType = Unnarrowed<Date | number | string>
type DateGetter<T extends NarrowedFrom<DateType> = NarrowedFrom<DateType>> = () => T;
//                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <-- assert narrowing

/** Defined elsewhere */
declare function getPublishDate(package: string): DateType;

/**
 * Prints a date returned by a DateGetter.
 */
function printDateFrom<F extends DateGetter>(getter: F) {
    console.log(getter());
}

printDateFrom(() => new Date()); // ok
printDateFrom(() => getPublishDate("unnarrowed"));
//                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <-- error, it returns unnarrowed DateType

AssertNarrowed

Asserts that the given type has been narrowed from an Unnarrowed<U>.

type AssertNarrowed<T, Options = {}>

Options:

{

    // If set to `true`, the assertion will only pass if the type is
    // narrowed to a single, non-union type.
    strict?: true | false
    
    // If set, this type will be returned when the assertion fails.
    failType: any;

}

Usage:

type GlasswareMaterial = Unnarrowed<'glass' | 'crystal'>;
interface Glassware<T extends GlasswareMaterial> {
    type: AssertNarrowed<T, {
        strict: true,
        failType: never,
    }>
}
const aWineGlass: Glassware<'glass' | 'crystal'> = {
    type: "glass"
//  ~~~~ <-- T is not strictly narrowed, so `type` is never
}

AssertStrictlyNarrowed

An alias for AssertNarrowed with the strict option set to true.

type AssertStrictlyNarrowed<T, Options = {}>

Utility Types

IsUnnarrowed

Checks if a type is Unarrowed<T>.

If the given type is any, an error type will be returned.

type IsUnnarrowed<T, WhenTrue=true, WhenFalse=false, WhenError=/* error */>

Usage:

type should_be_true  = IsUnnarrowed<Unnarrowed<string>>;
type should_be_false = IsUnnarrowed<string>;
type is_error        = IsUnnarrowed<any>;

IsNarrowed

Checks if a type is not Unarrowed<T>.

If the given type is any, the WhenTrue type will be returned.

type IsUnnarrowed<T, WhenTrue=true, WhenFalse=false, WhenError=WhenTrue>

Usage:

type should_be_true  = IsNarrowed<string>;
type should_be_false = IsNarrowed<Unnarrowed<string>>;
type also_true       = IsNarrowed<any>;

IsStrictlyNarrowed

Checks if a type is not Unarrowed<T>, and is not a type union.

If the given type is any, the WhenFalse type will be returned.

type IsUnnarrowed<T, WhenTrue=true, WhenFalse=false, WhenError=WhenTrue>

Usage:

type should_be_true  = IsStrictlyNarrowed<string>;
type should_be_false = IsStrictlyNarrowed<Unnarrowed<string>>;
type also_false_1    = IsStrictlyNarrowed<string | boolean>;
type also_false_2    = IsStrictlyNarrowed<any>;