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

typescript-assert-utils

v1.1.0

Published

Utility types for making assertions about TypeScript types

Downloads

89

Readme

typescript-assert-utils

Some importable TypeScript types that help you make assertions about your types.

You can use these tools in your TypeScript project to "test" your types. This can be useful to verify that the types are working as intended, or to ensure that you don't accidentally make breaking changes to your APIs from a types perspective.

Usage

  • Install typescript-assert-utils with npm or yarn or pnpm or whatever Node.js package manager you like.
  • In your TypeScript project, import assert and Is:
import { assert, Is } from "typescript-assert-utils";
  • Use assert and Is on your types:
function myFunc<T>(t: T) {
  return t;
}

const result = myFunc({ hello: true });
assert<Is<typeof result, { hello: boolean }>>();

Then, run TypeScript normally to check for errors:

$ tsc

At runtime, assert is a no-op function.

Excluding Assertions from Compiled Code

You may wish to exclude assertion-related code from your compiled code.

To do so, you may use the tsConfig stripInternal option, in conjunction with @internal comments:

import { assert, Is } from "typescript-assert-utils";

function myFunc<T>(t: T) {
  return t;
}

// This will remove the following code block.
/** @internal */
{
  const result = myFunc({ hello: true });
  assert<Is<typeof result, { hello: boolean }>>();
}

Or, you can put your assertions in a separate file. The separate file must be part of your TypeScript project (ie. included by your tsconfig.json), but shouldn't get imported by any of your normal code:

myFunc.ts:

export function myFunc<T>(t: T) {
  return t;
}

myFunc.type-assertions.ts:

import { assert, Is } from "typescript-assert-utils";

import { myFunc } from "./myFunc";

const result = myFunc({ hello: true });
assert<Is<typeof result, { hello: boolean }>>();

API

The following named exports are available:

assert

Use this to cause an error at type-checking type if T is not true.

export function assert<T>(): void;

Note that T must be exactly the type true; boolean will not work.

Example:

import { assert, Is } from "@suchipi/typescript-assert-utils";

function add(a: number, b: number) {
  return a + b;
}

const result = add(1, 2);

assert<Is<typeof result, number>>();
assert<Is<ReturnType<typeof add>, number>>();

// Error: Type 'false' does not satisfy the constraint 'true'.
assert<Is<typeof result>, string>();

Is

true when T is exactly U, and false otherwise.

export type Is<T, U> = true | false;

Note that, unlike the rest of TypeScript, Is does not consider any to be assignable to anything other than any. In other words, Is<any, string> is false.

Example:

import { assert, Is } from "@suchipi/typescript-assert-utils";

function add(a: number, b: number) {
  return a + b;
}

const result = add(1, 2);

assert<Is<typeof result, number>>();
assert<Is<ReturnType<typeof add>, number>>();

// Error: Type 'false' does not satisfy the constraint 'true'.
assert<Is<typeof result>, string>();

Not

false if T is true; true otherwise

export type Not<T> = false | true;

Example:

import { assert, Is, Not } from "@suchipi/typescript-assert-utils";

const something = 4;

// No error
assert<Is<typeof something>, number>();

// Error: Type 'false' does not satisfy the constraint 'true'.
assert<Is<typeof something>, string>();

// No error because we inverted the expression with Not
assert<Not<Is<typeof something>, string>>();

IsAny

true if T is the special type any; false otherwise

export type IsAny<T> = true | false;

Example:

import { assert, IsAny } from "@suchipi/typescript-assert-utils";

const something = 4;

// No error
assert<IsAny<any>>();

// Error: Type 'false' does not satisfy the constraint 'true'.
assert<IsAny<number>>();

IsNever

true if T is the special type never; false otherwise

export type IsNever<T> = true | false;

Example:

import { assert, IsNever } from "@suchipi/typescript-assert-utils";

const something = 4;

// No error
assert<IsNever<never>>();

// Error: Type 'false' does not satisfy the constraint 'true'.
assert<IsNever<number>>();

IsAssignable

true when Subtype extends Supertype, and false otherwise.

export type IsAssignable<Subtype, Supertype> = true | false;

Example:

import { assert, IsAssignable } from "@suchipi/typescript-assert-utils";

assert<IsAssignable<45, number>>();
assert<IsAssignable<"potato", string>>();

// Error: Type 'false' does not satisfy the constraint 'true'.
assert<IsAssignable<45, string>>();

License

MIT