February 2019
Beginner
694 pages
18h 4m
English
TypeScript introduces a type in order to indicate instances where something should never occur. A typical example of this is where a function will always throw an error, and as such will never return a value. Consider the following code:
function alwaysThrows() { throw "this will always throw"; return -1;}
Here, we have defined a simple function named alwaysThrows. The first line of this function throws an error, and as such, the second line of this function will never be executed. This is valid TypeScript, but does indicate that there is a flaw in our logic, as the return statement will never be executed. We can guard against this by using the return type of never in our function definition as follows:
function alwaysThrows(): never ...
Read now
Unlock full access