7. What's the difference between `never` and `void`?

The keyword void literally means emptiness. A function that returns the void type does not return any value. Assigning its execution to a variable is equivalent to setting that variable to the value undefined.

The never type signifies that a value will never occur. A function returning the never type never completes execution - for instance, it might handle an infinite loop or throw an exception.

const voidFunc = (): void => { const a = 1 + 1; } console.info(voidFunc()) // undefined const neverFunc = (): never => { // A function returning 'never' cannot have a reachable end point. const a = 1 + 1; } console.info(neverFunc()) // undefined const neverFunc2 = (): never => { const a = 1 + 1; return a; // Type 'number' is not assignable to type 'never'. }
devFlipCards 2024

Do you accept cookies?

Cookies are small amounts of data saved locally on you device, which helps our website - it saves your settings like theme or language. It helps in adjusting ads and in traffic analysis. By using this site, you consent cookies usage.