Assertions

An assertion is a condition that must be tested to confirm that a certain piece of code behaves as expected, or, in other words, to confirm conformance to a requirement.

Let's imagine that we are working as part of the Google Chrome development team, and we must implement the JavaScript Math API. If we are working on the pow method, the requirement could be something like, the Math.pow (base, exponent) function should return the base (the base number) to the exponent (the exponent used to raise the base) power-that is, base ^ exponent.

With this information, we could create the following implementation:

class MathAPI { public static pow(base: number, exponent: number) { let result = base; for (var i = 1; i < exponent; i++) { result ...

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.