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

kinda-type

v1.0.4

Published

A TypeScript utility type implementing a recursive Partial<T> for making mocking in unit tests less painful.

Downloads

7

Readme

Kinda

For when you want TypeScript to stop yelling at you because your mock is only "kinda" like the real thing.

This takes the built-in TypeScript Partial<T> type to the next level by making it recursive.

Example

Let's say we have some interfaces:

interface A {
  name: string;
  id: number;
  b: B;
}
interface B {
  color: string;
  isActive: boolean;
  c: C;
}
interface C {
  foo: "bar";
}

If you try to create a mock version of A, normally you would have to implement everything recursively:

const mockA: A = {
  name: "testName",
  id: 0,
  b: {
    color: "testColor",
    isActive: false,
    c: {
      foo: "bar",
    },
  },
};

This is not too bad in this limited example, but imagine mocking something like a DOM element, with hundreds of properties!

Luckily TypeScript has a built-in type called Partial<T> which is the same as T, but with all the properties marked optional. This is great, except it only works on one level, so when you are building your mocks, you end up with a lot of as casting to and from Partial types to avoid compiler errors.

let mockA: A;

beforeEach(() => {
  mockA = {
    b: {
      c: {} as Partial<C> as C
    } as Partial<B> as B
  } as Partial<A> as A;
});

This greatly harms the readability of your tests, and is generally pretty messy. Why can't we just tell TypeScript to not worry about the type being complete, while still getting type checking and autocompletion for the things you do add to your mocks?

This is where Kinda<T> comes in. Kinda tells TypeScript that everything (recursively) is optional. This means that the previous example can now be simplified greatly:

let mockA: A;

beforeEach(() => {
  mockA = {
    b: {
      c: {}
    }
  } as Kinda<A> as A;
});

Now you can focus on your testing and not how to make TypeScript stop yelling at your mocks.

Happy testing!

Acknowledgement

The implementation of Kinda<T> was largely based on the work of Pavel Husakouski from this StackOverflow post: https://stackoverflow.com/a/64060332/1396477. Renamed to Kinda from RecursivePartial for brevity.