title: What does the TypeScript asserts operator do?
body: <p>I have been toying around with TypeScript in the Node.js RTE, and I ran into a couple of type-operators (or at-least that's what I assume they are), and I don't know anything about them.</p>
  <p>The first operator was the <code>is</code> operator, which I was able to infer the purpose of, and I found documentation on, the second however; was the assertion operator. I thought at first I understood what its purpose was, but it was behaving in the way I had expected it to, I also failed to promptly find any thorough documentation on the subject, though I am certain it exists, as TS is well documented (especially if you consider what it is that the TS developers have to document).</p>
  <h4>EDIT:</h4>
  <blockquote>
  <p><em>Since asking this question, I have found documentation, and posted the Link in the comments below. Also another community member posted a link in the comments that is to an example that uses a TypeScript playground to document the similar subject of Assertion Functions.</em></p>
  </blockquote>
  <br>
  <h3>Where I see the Asserts Operator Being Used</h3>
  <p>I have included a snippet, and a type, that include the <code>asserts</code> operator. The two examples below outline situations where I ran into the <code>asserts</code> operator, and didn't fully understand the code, because I failed to fully understand what the <code>asserts</code> operator does.</p>
  <hr />
  <h6><em>Below is a code-snippet pulled from a guide on writing unit tests with TypeScript.</em></h6>
  <pre><code>function assertNonNullish&lt;TValue&gt;(
    value: TValue,
    message: string
  ): asserts value is NonNullable&lt;TValue&gt; {
    if (value === null || value === undefined) {
      throw Error(message);
    }
  }
  </code></pre>
  <h5><em><a href="https://mariusschulz.com/blog/assertion-functions-in-typescript#implementing-an-assertion-function" rel="nofollow noreferrer">Examples Source</a></em></h5>
  <hr />
  <br>
  <p>I also find the assert operator used to define the types of most assertion functions that included in the Node.js assertion library. This is what my hover widget shows for the assertions function <code>deepStrictEqual</code>'s type:
  <a href="https://i.stack.imgur.com/dqLEP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dqLEP.png" alt="enter image description here" /></a></p>
  <br>
  <h6>_...and this is the code for the assertion function's type (same as the image, just in code form):</h6>
  <pre><code>type DeepStrictEqual = &lt;T&gt;(actual: unknown, expected: T, message?: string | Error | undefined) =&gt; asserts actual is T;
  </code></pre>
  <br>
  <p>I really don't understand the purpose of using the assertion operator. I would like to figure out how it relates to typing JavaScript code, and writing unit-tests &amp; assertion functions in TypeScript, so I can better understand when (and where) I should use it.</p>