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'. }