Advertisement
Beginner TypeScript, Never, Unknown & Void Types

What does the 'never' type represent, and when does a function's return type become 'never'?

Short answer

'never' represents a value that can NEVER actually occur — a function's return type is inferred as 'never' when it always throws an exception, always enters an infinite loop, or otherwise never actually completes and returns control to the caller.

Save TypeScript mock interview
Advertisement
AModel answer
'never' represents a value that can NEVER actually occur — a function's return type is inferred as 'never' when it always throws an exception, always enters an infinite loop, or otherwise never actually completes and returns control to the caller.
function throwError(message: string): never {
  throw new Error(message);
}
function infiniteLoop(): never {
  while (true) {}
}
Real-world example
Typing a utility function that always throws (like an assertion helper) so the compiler knows code after calling it is unreachable.
Likely follow-ups
  • How is 'never' different from 'void', given both seem to relate to functions that 'don't return anything useful'?
Related topics