Saturday, November 3, 2018

TypeScript - DataTypes - never

With TypeScript 2.0 there are a few new types released. One of them is the never type. How to take the meaning of never type is: the code cannot be reached.

We typically use it in functions, where we know that the function will never return a value.

Consider the example:

// never
function noReturnValue():never {
throw new Error("This is an Error. Hence, the function will not return value.");
}

This function has a return-type never. because, it never finishes. This is different than the type void. It does not mean that the function returns nothing. It means, that this function does not return anything. Since, it throws an error.


Reference:

No comments:

Post a Comment