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

ts-opaque

v3.0.1

Published

Easy-to-use library that implements opaque types in TypeScript!

Downloads

40,379

Readme

ts-opaque

Build Status Downloads

Easy-to-use library that implements opaque types in TypeScript!

Installation

Install ts-opaque through npm:

$ npm install ts-opaque

Motivation

An opaque type, in TypeScript, is a type whose true structure is obfuscated to the compiler at compile-time. These types can make your code more type safe, secure, easier to refactor, and faster!

While Flow has an opaque keyword for creating opaque types, TypeScript does not; this package is my solution.

Usage

import Opaque from "ts-opaque";

interface User {
  readonly id: Opaque<number, User>;
  name: string;
}

interface Post {
  readonly id: Opaque<number, Post>;
  readonly authorId: User["id"];
  title: string;
  body: string;
}

let myUser: User = {
  // Error, type 'number' is not assignable to type 'Opaque<number, User>'
  id: 1,
  name: "John Doe",
};

myUser = {
  id: 1 as User["id"],
  name: "John Doe",
};

let myPost: Post = {
  // Error, type 'number' is not assignable to type 'Opaque<number, Post>'
  id: 1,
  // Error, type 'number' is not assignable to type 'Opaque<number, User>'
  authorId: 1,
  title: "ts-opaque",
  body: "It's a pretty cool package.",
};

myPost = {
  id: 1 as Post["id"],
  authorId: myUser.id,
  title: "ts-opaque",
  body: "It's a pretty cool package.",
};

There are other types and helper function ts-opaque exports as well! Check out the API below.

API

  • Opaque - Create an opaque type.

Helper Types

  • BaseType - Get the base type of an opaque type.
  • BrandType - Get the brand type of an opaque type.

Helper Functions

  • create - Cast a value to an opaque type.
  • widen - Widen an opaque type to its base type.

Related Works

Others have made fantastic packages to implement opaque types in TypeScript. While these implementations are flawless in their functionality, ts-opaque brands its opaque types with symbol keys, hiding them directly from your IDE's intellisense or its equivalent. ts-opaque is also distributed with helper functions and types to manipulate and use opaque types easily. Spread some love to these works:

License

This package is available as open source under the terms of the MIT License.