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

tsprism

v1.1.1

Published

a prism of type helpers and testing utilities for Typescript!

Downloads

221

Readme

TSPrism

A prism of type helpers and testing utilities for Typescript!

npm i -D tsprism

Testing Utilities

tsprism includes a set of utilities to help you test and document the expected output of generic, mapped or dynamic types.

Import

import { Expect, TypeOf, TS, ToBe, ToNotBe, ToEqual } from 'tsprism'

Expect

The main assertion wrapper for our type test cases. It needs to be assigned to a type for the TS compiler. It takes in a type which returns a boolean as argument T.

import { Expect } from 'tsprism'

type test = Expect<T>

TypeOf

Our main assertion type to make the comparison of our test cases. It takes three arguments to make the assertion.

  • Input - The type we want to test
  • Comparison Operator - The comperison type we want to use to make the assertion.
  • Expected Result - The type schema we expect
import { Expect, TypeOf, ToBe, ToNotBe, ToEqual } from 'tsprism'

type TEST = Expect<TypeOf<Input, Comparison, Expected>>

We can simplify this required type assignment (type TEST) by nesting multiple tests into a single test object type like so:

type TEST = {
  🟢 toBe: Expect<TypeOf<true, ToBe, boolean>>
  🔴 toNotBe: Expect<TypeOf<true, ToNotBe, boolean>>
  🔴 ToEqualFail: Expect<TypeOf<true, ToEqual, boolean>>
  🟢 ToEqual: Expect<TypeOf<true, ToEqual, true>>
}

This helps prevents pollution of the namepsace by test case types.

TS

This type is a semantic alias for TypeOf. They can be used interchangeably. It's purpose is to improve readability when the typeof keyword needs to be used before a runtime value in the test case.

For example:

import { Expect, TypeOf, TS, ToBe } from 'tsprism'

const myObj = { ts: 'test', prism: 'utilities' }
type Obj = typeof myObj

type TEST = {
  🟢 typeOf: Expect<TypeOf<Obj, ToBe, { ts: string, prism: string }>>
  🟢 ts: Expect<TS<typeof myObj, ToBe, { ts: string, prism: string }>> 
}

Comparison operators

ToBe

A comparison operator for primitive types (==).

import { Expect, TypeOf, ToBe } from 'tsprism'

const myObj = { ts: 'test', prism: 'utilities' }
type Obj = keyof typeof myObj

type TEST = {
  🟢 pass: Expect<TypeOf<Obj, ToBe, 'ts' | 'prism'>>
  🔴 fail: Expect<TypeOf<Obj>, ToBe, typeof myObj>
//                ˄˄ 🚁 Type 'false' does not satisfy the constraint 'true'.ts(2344)
}

ToNotBe

The inverse comparison operator for ToBe (!=).

import { Expect, TypeOf, ToNotBe } from 'tsprism'

const myObj = { ts: 'test', prism: 'utilities' }
type Obj = keyof typeof myObj

type TEST = {
  🟢 pass: Expect<TypeOf<Obj, ToNotBe, typeof myObj>>
  🔴 fail: Expect<TypeOf<Obj, ToNotBe, 'ts' | 'prism'>>
//                ˄˄ 🚁 Type 'false' does not satisfy the constraint 'true'.ts(2344)
}

ToEqual

Strict comparison operator for complex types (===).

import { Expect, TypeOf, ToBe, ToEqual } from 'tsprism'

const myObj = { ts: 'test', prism: 'utilities' } as const
type Obj = typeof myObj

type TEST = {
  🟢 pass:Expect<TypeOf<Obj, ToEqual, Readonly<{ ts: 'test', prism: 'utilities' }>>>
  🔴 fail: Expect<TypeOf<Obj, ToEqual, Readonly<{ ts: string, prism: string }>>>
//                ˄˄ 🚁 Type 'false' does not satisfy the constraint 'true'.ts(2344)
  🔴 fail2: Expect<TypeOf<Obj, ToEqual, { ts: 'test', prism: 'utilities' }>>
//                 ˄˄ 🚁 Type 'false' does not satisfy the constraint 'true'.ts(2344)
}

Both failing test cases would pass with the ToBe comparison operator.

Enjoy 🧪🚀