Never Link To Typescript

I am looking at Typescript documentation and have come across a data type. Never, until now, have I been able to understand exactly what they are trying to mean in a sentence, // Returning a function should never have an unreachable endpoint

function infiniteLoop(): never {
    while (true) {
    }
}

Can someone explain this to me?

+4
source share
1 answer

It says that if you never specify as a return type, the function return statement should not be reachable. For example, a function will always cause an error, or somewhere an infinite loop.

It is easy to visualize this if we rewrite the example as follows:

function infiniteLoop(): never {
    while (true) {
    }
    return 'this will never execute';
}
+5
source

Source: https://habr.com/ru/post/1657055/


All Articles