Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda 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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 9.09

bookVoid and Never Return Types

Deslize para mostrar o 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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3
some-alt