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 🙏

© 2026 – Pkg Stats / Ryan Hefner

biject

v3.0.1

Published

Type-safe bijective maps.

Readme

biject

Type-safe compile-time bijective maps for TypeScript.

NPM Version NPM Version

import { biject } from 'biject';

const example = biject<1 | 2, 'a' | 'b'>()(
  [
    [2, 'a'],
    [1, 'b'],
  ]
);

const foo: 'b' = example.map(1);
const bar: 2 = example.invert('a');

Installation

$ npm install --save biject
$ yarn add biject

To use this package, TypeScript version >= 4.1.0 is required as a peer dependency but using TypeScript >= 4.5 is recommended because it supports mapping sets of bigger sizes through tail-recursion elimination.

If you plan on mapping undefined or null values, make sure strictNullChecks is enabled to avoid them being inferred as any.

Usage

import { biject } from 'biject';

const example = biject([
  [1, 'one'],
  [2, 'two'],
  [3, 'three'],
]);

// The map can be used both ways.
example.map(2) // -> 'two'
example.invert('two') // -> 2

Maps that are not bijective will throw compilation errors.

// Fails at compile-time.
biject([
  [1, 'a'],
  [1, 'b'],
]);

// Fails at compile-time.
biject([
  [1, 'a'],
  [2, 'a'],
]);

The type of query operations is known at compile-time.

const example = biject([
  ['kek', 3141],
  ['lel', 5],
  ['pip', 927],
]);

// Compiles without error.
const five: 5 = example.map('lel');
const kek: 'kek' = example.invert(3141);

(element: 'kek' | 'pip') => {
  const mapsTo: 3141 | 927 = example.map(element);
}

:warning: Correct type inferrence only works if the type of the map given to biject exactly represents its run-time value. Make sure to not reference any values that TypeScript infers as unions.
:x: Avoid:

// Bad.
const union = 1 as 1 | 2;
biject([[union, 3]]);

This might lead to run-time errors.

If you are using TypeScript < 5.0, the input must be explicitly marked as const.

biject([[1, "a"], [2, "b"]] as const)

Since the introduction of const type parameters, this is not required anymore.

Type-guards

To check if a value is part of the map's domain or codomain (image), there are type-guards available.

// Define the bijection.
const example = biject([
  ['a', 1],
  ['b', 2],
  ['c', 3],
]);

// Get an unknown value from somewhere.
const element: unknown = document.getElementById('input').value;

if (example.isInDomain(element)) {
  // The `element` can now be used in `.map`.
  example.map(element);
}

if (example.isInImage(element)) {
  // The `element` can now be used in `.invert`.
  example.invert(element);
}

Specifying sets

The domain and codomain sets can explicitly be set as unions of elements. This will ensure that the mapping is complete and no elements have been forgotten.

type Domain = 1 | 2 | 3;
type Codomain = 'a' | 'b' | 'c';

// Fails at compile-time.
biject<Domain, Codomain>()(
  [
    [1, 'c'],
    [3, 'a'],
  ]
);

// Fails at compile-time.
biject<Domain, Codomain>()(
  [
    [1, 'c'],
    [1, 'b'],
    [3, 'a'],
  ]
);

// Compiles without error.
biject<Domain, Codomain>()(
  [
    [1, 'c'],
    [2, 'b'],
    [3, 'a'],
  ]
);

:warning: Note the extra parenthesis in biject<...>()(.... This is due to a restriction in TypeScript's type parameter inferrence.