Void 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.
123456789101112131415161718192021function 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.
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.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Awesome!
Completion rate improved to 9.09
Void and Never Return Types
Svep för att visa menyn
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.
123456789101112131415161718192021function 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.
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.
Tack för dina kommentarer!