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

jestype

v1.0.0

Published

Typescript Type Tester to use with Jest

Readme

Jestype - TypeScript Type Tester for Jest

Jestype is a TypeScript utility designed to facilitate type testing within Jest, providing developers with tools to assert and validate type behaviors in TypeScript projects. At its core, expectType serves as the type-level counterpart to Jest's expect function, enabling type assertions in a manner analogous to value-based testing.

Getting Started

Install Jestype in your development environment with npm:

npm install jestype --save-dev

Then, import and use expectType in your Jest tests to validate types.

expectType

expectType is the primary function of Jestype and acts as the type-level equivalent of Jest's expect.

It allows type assertions for a specific type T, providing a fluent API for asserting type properties and relationships.

Usage

import { expectType } from 'jestype';

// Assert that a type is exactly another type
expectType<string>().toBe<string>();

// Assert that a type extends another type
expectType<string>().toExtend<string | number>();

// Negation example
expectType<string>().not.toBe<number>();

Methods

  • .toBe<S>(): Asserts that the tested type T is exactly the same as type S.

  • .toExtend<S>(): Asserts that the tested type T extends type S.

  • .toBeOfUnion<S>(): Asserts that the tested type T is part of the union S

  • .toHave<S>(): Asserts that the tested type T has the same properties as type S.

  • .toBePartOf<S>(): Asserts that the tested type T is a subset of type S.

  • .toHaveRequired<K extends string[]>(): Asserts that the tested type T has all the required keys specified in K.

  • .toHaveOptional<K extends string[]>(): Asserts that the tested type T has all the optional keys specified in K.

  • .not: Inverts the subsequent type assertion, allowing for negated checks.

Examples

.toBe<S>

Asserts that the tested type T is exactly the same as type S.

// Should pass: string is exactly a string
expectType<string>().toBe<string>();

// Should fail: string is not a number
expectType<string>().toBe<number>();

.toExtend<S>

Asserts that the tested type T extends type S.

/* Using Interfaces*/
interface Base {
    baseProp: string;
}

interface Extended extends Base {
    extendedProp: number;
}

// Should pass: Extended extends Base
expectType<Extended>().toExtend<Base>();

// Should fail: Base does not extend Extended
expectType<Base>().toExtend<Extended>();


/* Using Unions */

// Should pass: string is assignable to string | number 
expectType<string>().toExtend<string | number>();

// Should fail: string | number is not assignable to string 
expectType<string | number>().toExtend<string>();

.toBeOfUnion<S>

Asserts that the tested type T is part of the union S

Exactly the same as toExtend, but lends to a more intuitive sounding type assertion for unions

// Should pass: `string` is part of union `string | number`
expectType<string>().toBeOfUnion<string | number>();

// Should fail: `string` is not in union `string | number`
expectType<string | number>().toBeOfUnion<string>();

.toHave<S>

Asserts that the tested type T has the same properties as type S.

interface Person {
    name: string;
    age: number;
}

// Should pass: Person has name and age properties
expectType<Person>().toHave<{ name: string; age: number; }>();

// Should fail: Person does not have a salary property
expectType<Person>().toHave<{ salary: number; }>();

.toBePartOf<S>

Asserts that the tested type T is a subset of type S.

interface Vehicle {
    make: string;
    model: string;
    year: number;
}

interface Car {
    make: string;
    model: string;
}

// Should pass: Car is part of Vehicle
expectType<Car>().toBePartOf<Vehicle>();

// Should fail: Vehicle is not a subset of Car
expectType<Vehicle>().toBePartOf<Car>();

.toHaveRequired

Asserts that the tested type T has all the required keys specified in K.

interface User {
    id: number;
    name: string;
    email?: string;
}

// Should pass: User has required keys id and name
expectType<User>().toHaveRequired<['id', 'name']>();

// Should fail: User does not have a required key 'password'
expectType<User>().toHaveRequired<['password']>();

.toHaveOptional<S>

Asserts that the tested type T has all the optional keys specified in K.

interface Product {
    id: number;
    name: string;
    description?: string;
    price?: number;
}

// Should pass: Product has optional key 'description'
expectType<Product>().toHaveOptional<['description']>();

// Should fail: Product does not have an optional key 'weight'
expectType<Product>().toHaveOptional<['weight']>();

.not

Inverts the subsequent type assertion, allowing for negated checks.

// Should pass: string is not a number
expectType<string>().not.toBe<number>();

// Should fail: string is not a number
expectType<string>().<number>();

Type Utilities

Jestype also includes Utility Types that expectType is built on:

Logical Types

  • And<T, U>: Logical AND of T and U.

  • Or<T, U>: Logical OR of T and U.

  • Not<T>: Logical NOT of T.

  • Nand<T, U>: Logical NAND of T and U.

  • Nor<T, U>: Logical NOR of T and U.

  • Xor<T, U>: Logical XOR of T and U.

  • Eq<T, U>: Checks if T and U are equivalent.

  • All<Items>: True if all Items are true.

  • Any<Items>: True if any of Items are true.

  • None<Items>: True if none of Items are true.

  • Switch<Condition, Inverted>: Evaluates Condition and inverts the result if Inverted is true.

  • If<Condition, Then, Else>: Returns Then if Condition is true, otherwise Else.

ExtendsTypes

  • Extends<T, S>: Determines if type T extends type S.

  • BiExtends<T, S>: Determines if types T and S are mutually extendable (bi-directional extends).

Key Types

  • RequiredKeys<T>: Extracts the required keys of type T.

  • OptionalKeys<T>: Extracts the optional keys of type T.

  • AlwaysKeys<T>: Extracts keys of T that are always present.

Primitive Types

  • IsAny<T>: Determines if type T is any.

  • IsNever<T>: Determines if type T is never.

  • IsUnknown<T>: Determines if type T is unknown.

  • IsEmpty<T>: Determines if type T is an empty object ({}).

  • IsPrimitive<T>: Determines if type T is a primitive type.

Branding

  • Brand<T>: Creates a branded representation of type T, useful for complex type analysis.