Advertisement
Intermediate TypeScript, Never, Unknown & Void Types

Why does assigning a function that returns a specific value (like `() => string`) to a variable typed as `() => void` compile without error?

Short answer

TypeScript special-cases 'void' function-type COMPATIBILITY: a function returning any actual value CAN be assigned where a void-returning function is expected, because the caller has explicitly signaled it will ignore whatever comes back — this allows patterns like passing…

Save TypeScript mock interview
Advertisement
AModel answer
TypeScript special-cases 'void' function-type COMPATIBILITY: a function returning any actual value CAN be assigned where a void-returning function is expected, because the caller has explicitly signaled it will ignore whatever comes back — this allows patterns like passing Array.prototype.push (which returns a number) as a void-expecting callback without extra wrapping.
const callback: () => void = () => 'some value'; // allowed, even though it returns a string
const arr: number[] = [];
const pushWrapper: () => void = () => arr.push(42); // 'push' returns a number, still OK here
Real-world example
Passing existing functions with meaningful return values (like array mutator methods) directly as void-expecting callbacks without a wrapper.
Likely follow-ups
  • Does this same leniency apply to a variable EXPLICITLY typed as 'undefined' instead of 'void'?
Related topics