title: How to get a Jest custom matcher working in typescript?
body: <p>I regularly have unit tests where I need to compare two moment objects. I'd us moment's built-in function moment.isSame(moment) to compare them. However, this means my assertion will look like this:</p>
  
  <p><code>expect(moment1.isSame(moment2)).toBeTrue();</code></p>
  
  <p>I didn't quite like this, especially because the failure message will be less informative. Hence, I wanted to write a custom jest matcher "toBeSameMoment". The following code seems to compile at least:</p>
  
  <pre><code>import moment from "moment";
  
  declare global {
    namespace jest {
      interface MomentMatchers extends Matchers&lt;moment.Moment&gt; {
        toBeSameMoment: (expected: moment.Moment) =&gt; CustomMatcherResult;
      }
    }
  }
  
  expect.extend({
    toBeSameMoment(received: moment.Moment, expected: moment.Moment): jest.CustomMatcherResult {
      const pass: boolean = received.isSame(expected);
      const message: () =&gt; string = () =&gt; pass ? "" : `Received moment (${received.toISOString()}) is not the same as expected (${expected.toISOString()})`;
  
      return {
        message,
        pass,
      };
    },
  });
  </code></pre>
  
  <p>However, I can't really get it to work in my unit test...
  When I try the following test code:</p>
  
  <pre><code>import moment from "moment";
  import "../jest-matchers/moment";
  
  describe("Moment matcher", () =&gt; {
  
    test("should fail", () =&gt; {
      const moment1 = moment.utc();
      const moment2 = moment();
  
      expect(moment1).toBeSameMoment(moment2);
    });
  
  });
  </code></pre>
  
  <p>...then I get the following error:</p>
  
  <pre><code>error TS2339: Property 'toBeSameMoment' does not exist on type 'JestMatchersShape&lt;Matchers&lt;void, Moment&gt;, Matchers&lt;Promise&lt;void&gt;, Moment&gt;&gt;'.
  </code></pre>
  
  <p>I don't quite get this error, though. For example, what is the void type this is referring to? I've tried googling about it, but didn't really find a good guide or so. I did take notice of this question: <a href="https://stackoverflow.com/questions/59495104/how-to-let-know-typescript-compiler-about-jest-custom-matchers">How to let know typescript compiler about jest custom matchers?</a>, which seems to basically be a duplicate, but apparently not clear enough, yet. </p>
  
  <p>I have jest types included in my tsconfig</p>