Tackling TypeScript
Please support this book: buy it or donate
(Ad, please don’t block.)

6 Notation used in this book



This chapter explains functionality that is used in the code examples, but not part of TypeScript proper.

6.1 Test assertions (dynamic)

The code examples shown in this book are tested automatically via unit tests. Expected results of operations are checked via the following assertion functions from the Node.js module assert:

This is an example of using these assertions:

import {strict as assert} from 'assert';

assert.equal(3 + ' apples', '3 apples');

assert.deepEqual(
  [...['a', 'b'], ...['c', 'd']],
  ['a', 'b', 'c', 'd']);

assert.throws(
  () => eval('null.myProperty'),
  TypeError);

The import statement in the first line makes use of strict assertion mode (which uses ===, not ==). It is usually omitted in code examples.

6.2 Type assertions (static)

You’ll also see static type assertions.

%inferred-type is just a comment in normal TypeScript and describes the type that TypeScript infers for the following line:

// %inferred-type: number
let num = 123;

@ts-expect-error suppresses static errors in TypeScript. In this book, the suppressed error is always mentioned. That is neither required in plain TypeScript, nor does it do anything there.

assert.throws(
  // @ts-expect-error: Object is possibly 'null'. (2531)
  () => null.myProperty,
  TypeError);

Note that we previously needed eval() in order to not be warned by TypeScript.