Alex's Slip-box

These are my org-mode notes in sort of Zettelkasten style

Mocking interfaces

One of the problems with complex interfaces is that they can make mocking test data cumbersome in unit tests. TypeScript has some ways around this.

# Use the Utility Types

Check out Utility Types documentation. Here’s a simple example of using the Omit utility.

I was dealing with an interface that had a required property that was itself a complex data structure with further nested objects. Kind of like this:

interface Thing {
  prop1: string;
  prop2: string;
  complexData: ComplexData

In my test, I didn’t need complexData at all. So, it would have been really heavy-handed to mock that data in my unit test. Using the Omit utility, I was able to create an interface with all properties except the complexData property.

type MockThing = Omit<Thing, 'complexData'>;

const thing: MockThing = {
  prop1: 'foo',
  prop2: 'bar'

There’s a bunch of other utilities. See the TypeScript utility types docs for all of them.