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

@sbnc/expect-type-extensions

v1.0.0-rc.0

Published

Additional type testing methods for the `expect-type` test suite.

Downloads

3

Readme

Expect Type Extensions

This package provides additional type testing methods for the expect-type compile-time test suite for types.

Goal

Sometimes, we want to test whether two types can be used interchangeably, i.e., ensure that while they may not be equal, they can be assigned to each other.

For example:

type Foo = { a: string, b: number }
type Bar = { a: string } & { b: number }

In practice, we can assign anything of type Foo to anything of type Bar and vice versa. So, from a practical point of view, these types are compatible. We naturally want to test for this kind of relationship between types.

Challenge

While expect-type is a great utility, it is a bit suboptimal for this particular case. We cannot use a single toEqualTypeOf, as both of these would result in an error:

expectTypeOf<Foo>().toEqualTypeOf<Bar>() // error, but we want a pass
expectTypeOf<Bar>().toEqualTypeOf<Foo>() // error, but we want a pass

This means we would need to resort to two toMatchTypeOf tests:

expectTypeOf<Foo>().toMatchTypeOf<Bar>()
expectTypeOf<Bar>().toMatchTypeOf<Foo>()

We need both lines because in the following case, only one of them shows the issue:

type Baz = { a: string, b: number, c: boolean }

expectTypeOf<Baz>().toMatchTypeOf<Foo>() // seems okay
expectTypeOf<Foo>().toMatchTypeOf<Baz>() // but this line shows the issue with Baz being incompatible

However, it can be a bit hard to read and maintain: always writing double test lines for this kind of scenario.

Solution

This utility provides a single .toMatchEachOther<>() method which can be used to test for this kind of relationship between types.

With this in place, we can now write what took us 4 tests before in only 2 lines:

expectTypeOf<Foo>().toMatchEachOther<Bar>() // pass
expectTypeOf<Foo>().toMatchEachOther<Baz>() // error, Baz is not compatible

If the second one is the expected behaviour, we can use the .not.toMatchEachOther<>() method instead:

expectTypeOf<Foo>().not.toMatchEachOther<Baz>() // pass

Usage

Installation

Install using npm or your choice of package manager, e.g.:

npm install --save-dev @sbnc/expect-type-extensions

Importing

To use the new utilities, make sure to import this package along with expect-type, e.g.:

import { expectTypeOf } from 'expect-type'
import '@sbnc/expect-type-extensions'

Note: There is no need for a {...} from part in the import statement.


Bence Szalai - https://sbnc.eu/