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

soit

v2.0.0

Published

Create a type guard from a list of literals.

Downloads

98

Readme

Soit

typescript codecov prettier npm

Soit (French for: either) is like an enhanced Set() function which simplifies type narrowing and aims to replace TypeScript enums.

Motivation

The enum feature of TypeScript is not ideal. It does not provide type guards and is not iterable.

I wanted a simple lib which provides a way to narrow type to a given set of values and can be iterated.

Inspired from the enum feature of zod.

Declaration

A Soit instance can be created by passing literals (string, number or boolean) in an array to the Soit function.

const isWarmColor = Soit(["red", "orange"]);

You can infer the corresponding union using the Infer "helper" provided by the lib.

type WarmColor = Infer<typeof isWarmColor>; // infers "red" | "orange"

You can pass any string, number or boolean you want.

const isColdColor = Soit(["one", 1, true]);

Soit instances are iterable and can be used to create new definitions.

const isColor = Soit([...isWarmColor, "green", "blue"]);

type Color = Infer<typeof isColor>; // infers "red" | "orange" | "green" | "blue"

Guard

A Soit instance is intended to be used as a type guard:

function handleColor(color: Color) {
    if(isWarmColor(color)) {
        // color can be "red" | "orange"
    }
    // color can be "blue" | "green"
}

Array utils

Because the Soit instance is iterable, you can access the corresponding array:

const colors = Array.from(isColor);

You may prefer this syntax:

const colors = [...isColor];

map and forEach can be used without Array.from().

isColor.forEach((color) => console.log(color));

const uppercaseColors = isColor.map(color => color.toUpperCase());

Set utils

.subset([])

You can create subsets using the subset method.

const isWarmColor = isColor.subset(["red", "orange"]);

This checks on build time that "red" and "orange" do exist in the isColor instance.

.extend([])

You can extend an existing Soit instance using the extend method.

const isColor = isWarmColor.extend(["blue", "green"]);

.difference([])

You can create a new instance without the specified values using the difference method.

const isColdColor = isColor.difference(["red", "orange", "yellow"]);

The given array don't need to be a subset and can contain values that don't exist in the initial instance.

Troubleshoot

Type 'string' is not assignable to type 'never'. ts(2345)

You are maybe trying to create a new Soit instance using a named array.

const warmColors = ["red", "orange"];
const isWarmColor = Soit(warmColors); // error ts(2345)

Soit throw this error to prevent passing an unknown set of value (i.e. string[]). The solution here is to use the as const declaration in order to freeze the values and allow a proper type inference.

const warmColors = ["red", "orange"] as const;
const isWarmColor = Soit(warmColors);