![]() |
In this article, we are going to declare the functions that throw errors in TypeScript. A function can throw an error if the logic is not correct or you can deliberately declare the function that always throws an error. A function can be created with the different return types with the required type of value it returns. We will create the error functions with two types of return values as discussed below: Declare using the void type return valueA function whose return type is not defined will automatically return a void type value. This type of function will throw an error if they accidentally return something. Syntax:function function_name(): void/empty{}
Example: The below example will show how you can create a function that throws an error with a void type return value. Javascript
Output: This is a function that throws error Declare using the never type return valueThe function with never type return value will never return anything after the function execution. Syntax:function function_name(): never {}
Example: The below example will illustrate how to declare error functions with never type return value. Javascript
Output: 18 |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |