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

narrow-types

v1.4.5

Published

A collection of branded Typescript types, using [zod](https://github.com/colinhacks/zod/) as the underlying validation library.

Downloads

74

Readme

narrow-types

A collection of branded Typescript types, using zod as the underlying validation library.

import { emailString, EmailString } from "narrow-types";

// The address parameter must be a validated email address
function sendEmail(address: EmailString, subject: string) {
  // ...
}

// This function accepts any string
function logString(message: string) {
  // ...
}

// myEmail is an EmailString
const myEmail = emailString.parse("[email protected]");

sendEmail(myEmail, "Hello"); // Works
logString(myEmail); // Works as well. EmailStrings are strings as far as JS is concerned.

sendEmail("john@doe", "Hello"); // Error: address is not an EmailString

Installation

npm install narrow-types

Rationale

So you are using Zod to make an air tight boundary between the outside and your type safe core. Say you have an endpoint that looks something like this:

const signupBody = z.object({
  email: z.string().email(),
  password: z.string().min(8),
});

autoRoutes.post("/signup", async (ctx) => {
  const body = signupBody.parse(ctx.req.body);

  ctx.state.db.accounts.insert({
    site: ctx.state.site,
    email: body.email,
    password: await hash(body.password),
  });

  ctx.body = { success: true };
});

Great! You have a nice, clean endpoint. You are certain that the email inserted is a valid email address and that the password is at least 8 characters. But what if you would like to extract a function for signing people up so it could be used elsewhere? You could do something like this:

async function signup(
  db: Database,
  site: string,
  email: string,
  password: string
) {
  db.accounts.insert({
    site,
    email,
    password: await hash(password),
  });
}

and call that. But now you are no longer sure that the provided email is valid. You could move the Zod validation step into the function, but if you want to keep it in the endpoint as well for some reason, you will now be validating twice at runtime which is a performance cost, and not very pretty either.

Instead, you can use the EmailString type from this library:

import { EmailString } from "narrow-types";

async function signup(
  db: Database,
  site: string,
  email: EmailString,
  password: string
) {
  db.accounts.insert({
    site,
    email,
    password: await hash(password),
  });
}

Now, the type of email is EmailString which is a string that has been validated to be a valid email address. To use it, we modify our Zod schema and endpoint to look like this:

import { emailString } from "narrow-types";

const signupBody = z.object({
  email: emailString,
  password: z.string().min(8),
});

authRoutes.post("/signup", async (ctx) => {
  const body = signupBody.parse(ctx.req.body);

  signup(ctx.state.db, ctx.state.site, body.email, body.password);

  ctx.body = { success: true };
});

The EmailString type is just a branded string, incurring no runtime cost. It is just a way to tell Typescript that the string is a valid email address. This allows us to use the same type in both the endpoint and the function, and we can be sure that the email address is valid in both places. Any function that expects a plain string will accept an EmailString as well, but not the other way around.

This library contains a number of types for common use cases, all compatible with Zod.

Types

Simple Number types

  • Integer
  • Int2
  • Int4
  • Int8
  • PositiveNumber
  • NegativeNumber
  • NonPositiveNumber
  • NonNegativeNumber
  • FiniteNumber
  • SafeNumber

Simple String types

  • NonEmptyString

ID types

  • UUIDString
  • CUIDString
  • CUID2String
  • ULIDString

Network types

  • EmailString
  • URLString
  • IPString
  • IPV4String
  • IPV6String
  • MACAddressString
  • JwtString

CSS types

  • HexColorString
  • HexColorWithAlphaString

Misc types

  • EmojiString
  • DateTimeString
  • JSONString