Type Checking with Assertion Functions in TypeScript
An assertion function is a runtime check that identifies the type of unknown input. TypeScript's compiler assumes the input is of the type claimed by the assertion function's signature. Assertion functions are useful for uncertain values, like user input, and generate runtime checks. They can raise errors if the input doesn't meet criteria.
An assertion function is the implementation of a runtime check that is able to identify the type of unknown input. When the conditions of the assertion functions are passed, TypeScript's compiler will then assume that the input is of the type claimed by the signature of the assertion function.
Contents
Assertion Function
The TypeScript code below examines an input
of type unknown
. If the assertUser
function does not produce an error, TypeScript's compiler will assume that the input
is of type User
as specified in the signature of the assertion function with asserts input is User
:
Benefits
- Assertion functions are useful in situations where the type of a value is uncertain, especially when the value originates from external sources such as user input or third-party libraries.
- Unlike type assertions, which are only used by the compiler to check the type of a value, assertion functions generate JavaScript code that performs the check at runtime.
- Assertion functions can check that the input meets certain criteria, and if it doesn't, they can raise an error, preventing the rest of the code from executing in an undefined state.
Disadvantages
- Because the checks of an assertion function are created by the user, it is possible for them to contain errors or omitted conditions.
Incorrect Assertion Function
Assertion Functions in Node.js
If your code is running in a Node.js environment and you have installed @types/node
, you can make use of Node's built-in module assert:node
to do assertions on your variables: