Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Void and Never Return Types | Typing Functions in TypeScript
TypeScript Functions and Parameters

bookVoid and Never Return Types

When working with TypeScript, you will often need to indicate what type of value a function returns. Two special return typesβ€”void and neverβ€”play important roles in expressing intent about a function's behavior. Understanding these types helps you write clearer, safer code and avoid mistakes that could occur when a function's return behavior is misunderstood.

123456789101112131415161718192021
function logMessage(msg: string): void { console.log("Log:", msg); } function throwError(): never { throw new Error("Something went wrong!"); } // Valid calls logMessage("System started"); // Output: Log: System started logMessage("User logged in"); // Output: Log: User logged in // Invalid (failure) calls // logMessage(); // Error: Expected 1 argument, but got 0. // logMessage(123); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. // Correct usage of throwError throwError(); // Uncommenting this will immediately throw an error and stop execution. // Invalid (failure) calls // throwError("Oops"); // Error: Expected 0 arguments, but got 1.
copy

Use the void type when a function does not return any value. For instance, the logMessage function above simply prints a message to the console and does not return anything, so its return type is void. On the other hand, use the never type when a function cannot return to its caller under any circumstances. The throwError function demonstrates this: it always throws an error, so execution never reaches the end of the function or returns a value. Functions that always throw errors or contain infinite loops are appropriate places to use the never return type. By using void and never thoughtfully, you make your function contracts explicit and help TypeScript catch potential errors at compile time.

question mark

Which TypeScript return type should you use for a function that never returns to its caller, such as one that always throws an error or contains an infinite loop?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain more about when to use `void` vs `never` in real-world scenarios?

What happens if I accidentally use the wrong return type in TypeScript?

Can you show more examples of functions that use `never`?

Awesome!

Completion rate improved to 9.09

bookVoid and Never Return Types

Swipe to show menu

When working with TypeScript, you will often need to indicate what type of value a function returns. Two special return typesβ€”void and neverβ€”play important roles in expressing intent about a function's behavior. Understanding these types helps you write clearer, safer code and avoid mistakes that could occur when a function's return behavior is misunderstood.

123456789101112131415161718192021
function logMessage(msg: string): void { console.log("Log:", msg); } function throwError(): never { throw new Error("Something went wrong!"); } // Valid calls logMessage("System started"); // Output: Log: System started logMessage("User logged in"); // Output: Log: User logged in // Invalid (failure) calls // logMessage(); // Error: Expected 1 argument, but got 0. // logMessage(123); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. // Correct usage of throwError throwError(); // Uncommenting this will immediately throw an error and stop execution. // Invalid (failure) calls // throwError("Oops"); // Error: Expected 0 arguments, but got 1.
copy

Use the void type when a function does not return any value. For instance, the logMessage function above simply prints a message to the console and does not return anything, so its return type is void. On the other hand, use the never type when a function cannot return to its caller under any circumstances. The throwError function demonstrates this: it always throws an error, so execution never reaches the end of the function or returns a value. Functions that always throw errors or contain infinite loops are appropriate places to use the never return type. By using void and never thoughtfully, you make your function contracts explicit and help TypeScript catch potential errors at compile time.

question mark

Which TypeScript return type should you use for a function that never returns to its caller, such as one that always throws an error or contains an infinite loop?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 3
some-alt