Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

void is a special type in TypeScript representing the absence of a specific type or value. It is the conceptual opposite of any and is primarily utilized as the return type for functions that terminate normally but do not explicitly return a value to the caller.
function executeProcess(): void {
    console.log("Processing...");
    // Implicitly returns undefined
}

Type Compatibility and Assignment

Variables can be explicitly typed as void, though this is uncommon. From a type-checking perspective, a void type is only compatible with undefined. If the compiler flag strictNullChecks is disabled, void is also compatible with null.
let uninitialized: void;

uninitialized = undefined; // Valid
uninitialized = null;      // Valid ONLY if strictNullChecks is false
uninitialized = "string";  // Error: Type 'string' is not assignable to type 'void'

Contextual Typing Behavior

TypeScript enforces different strictness levels for void depending on whether it is used in a literal function declaration or as a contextual type signature. 1. Literal Function Declarations When a function is explicitly declared to return void, the TypeScript compiler strictly enforces that the function must not return a value.
function strictVoid(): void {
    return true; // Error: Type 'boolean' is not assignable to type 'void'.
}
2. Contextual Function Types When void is used in a function type alias or interface, it alters its behavior. A contextual type of () => void does not mean the function cannot return a value; rather, it indicates that the caller must ignore any returned value.
type VoidCallback = () => void;

// Valid: The implementation returns a number, but the type signature 
// guarantees the caller will treat the result as void.
const pushToArray: VoidCallback = () => [1, 2, 3].push(4);

const result = pushToArray(); 
// 'result' is strictly typed as 'void'. 
When a variable is typed as void (like result above), TypeScript restricts operations that depend on the value having a specific type or being tested for truthiness. However, it does not strictly forbid all usage in expressions. A void variable can still be passed to functions accepting any or unknown, evaluated in a typeof expression, or assigned to another variable.

void vs. never

While both types represent non-values, their control flow implications differ fundamentally:
  • void: The function executes completely and returns control to the caller, either by reaching the end of its block implicitly or by executing an explicit early return without a value.
  • never: The function is interrupted and never successfully returns control to the caller (e.g., it throws an exception or enters an infinite loop).
function returnsVoid(): void {
    return; // Control flow returns here explicitly without a value
}

function returnsNever(): never {
    throw new Error("Halt"); // Control flow terminates here
}
Master TypeScript with Deep Grasping Methodology!Learn More