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

v1.0.3

Published

A helper for declaring TypeScript literals so that they conform to a type constraint but are still inferred to their specific type.

Downloads

2

Readme

type-constraint

A helper for declaring TypeScript literals so that they conform to a type constraint but are still inferred to their specific type.

Installation

Using NPM:

$ npm install type-constraint

Using Yarn

$ yarn add type-constraint

Usage

Sometimes it's useful to infer the type of an object literal, while still having the literal conform to a type constraint.

Example:

Consider this Api interface

type Handler<T> = (params: any) => T;

interface Api {
  greet: Handler<string>;
  cat: Handler<string>
  ultimateQuestion: Handler<number>;
}

If an interface like this (but typically much larger) is being implemented in parts, there is no built-in way of defining partial implementations in different objects, while making sure each part conforms to its share of the interface, and also ensuring the combined parts make out the complete, correct interface.

Without type-constraint

Inferring parts: :x:

const subImpl1 = {
  greet: () => 'Hello, world!',
  cat: () => 'Meow!',
} // type is inferred but not type checked against Api

const subImpl2 = {
  ultimateQuestion: () => 42
} // type is inferred but not type checked against Api

const impl: Api = {...subImpl1, ...subImpl2}; // combined implementation is type checked against Api

Using Partial: :x:

const subImpl1: Partial<Api> = {
  greet: () => 'Hello, world!',
  cat: () => 'Meow!',
} // typed as Partial<Api>, we lose track of _which_ part is implemented by this object

const subImpl2: Partial<Api> = {
  ultimateQuestion: () => 42
} // typed as Partial<Api>, we lose track of _which_ part is implemented by this object

const impl: Api = {...subImpl1, ...subImpl2}; // Does not compile since the compiler does not know that subImpl1 & subImpl2 makes out the complete interface.

With type-constraint

import Constraint from 'type-constraint';

const api = Constraint.of<Api>();

const subImpl1 = api.pick({
  greet: () => 'Hello, world!',
  cat: () => 'Meow!',
}); // Type is inferred as Pick<Api, 'greet' | 'cat'> while the literal is still type-checked against Api :+1:

const subImpl2 = api.pick({
  ultimateQuestion: () => 42
}); // Type is inferred as Pick<Api, 'ultimateQuestion'> while the literal is still type-checked against Api :+1:

const impl: Api = {...subImpl1, ...subImpl2}; // subImpl1 & subImpl2 makes out the complete interface! :+1:

All the requirements are fulfilled! :+1:

  • Sub-parts are type-checked against the full interface to eliminate spelling mistakes and incorrectly implemented methods :white_check_mark:
  • Sub-parts have their type inferred to their specific part of the full interface :white_check_mark:
  • Combined sub-parts can be verified to make out the full interface at compile time :white_check_mark:

Examples of errors found in compile time using type-constraint:

  1. Incorrectly implemented method (wrong return type)
const subImpl1 = api.pick({
  greet: () => 42, // Error: Type number is not assignable to string
  cat: () => 'Meow!',
});
  1. Spelled method incorrectly:
const subImpl2 = api.pick({
  ultimateQestion: () => 42 // Error: Object literal may only specify known properties, but ultimateQestion does not exist in Pick<Api, keyof Api>. Did you mean to write ultimateQuestion?
});