Search⌘ K

Assertion Functions

Explore how assertion functions in TypeScript enhance type checking by verifying values at runtime. Understand how to write these functions to throw exceptions on invalid types, aiding in safer code especially when integrating JavaScript codebases with TypeScript.

We'll cover the following...

The assertion function is new to TypeScript version 3.7. It aims to not only ensure that a value is of a specific type but also to throw at (runtime) an exception if it does not respect the expected type. The assertion function use cases are more oriented for a codebase that is coming from Javascript where asserting types was a common practice. With ...