. Advertisement .
..3..
. Advertisement .
..4..
Are you having trouble with how to get the return Type of a Function in TypeScript? Don’t be concerned! In this article, we’ll learn about the function and offer some solutions to answer the question. Let’s get started!
The way to Get the return Type of a Function in TypeScript?
Firstly, we will get in touch with basic information about a Function in TypeScript.
TypeScript functions are the foundation for readable, maintainable, and reusable code.
Type annotations can be used in function parameters and return the result in TypeScript.
Method 1: Utilize returning function
Functions may also return value as well as control to the caller. These functions are known as returning functions. Take a look at the syntax below:
function function_name():return_type {
//statements
return value;
}
A return statement is required at the end of a returning function.
Any valid data type can be used as the return type.
The data type of the value returned must match the function’s return type.
A function could only return one value. This means each function can only have one return statement.
The ReturnType type helps determine the kind of function which may or may not have been written by the user and may or may not be in a third-party library.
Applying that syntax to the script, we will have the following commands:
//function defined
function greet():string { //the function returns a string
return "Hello World"
}
function caller() {
var msg = greet() //function greet() invoked
console.log(msg)
}
//invoke function
caller()
The output will appear as:
Hello World
Method 2: Utilize add() function and void type
function add(a: number, b: number): number {
return a + b;
}
Look at the example above; as you can see, when you call the add() function, the TypeScript compiler verifies that all arguments passed to the function are numbers. In the preceding example, you can only give numbers, not values of other types. The following code will fail because it uses two strings instead of two numbers in the add() function.
let sum = add('10', '20');
After that, an error will likely appear.
error TS2345: Argument of type '"10"' is not assignable to parameter of type 'number'
To fix that, the void type should be used as the return type because it indicates that the function does not return any value.
function echo(message: string): void {
console.log(message.toUpperCase());
}
When the return type is not annotated, TypeScript will attempt to infer an appropriate type.
function add(a: number, b: number) {
return a + b;
}
As you can see, this method is not perfect; errors can still occur and take a long time to resolve. So please take a close look and select the technique you believe will work best in your situation.
Conclusion
We hope you have found the best way to Get the return Type of a Function in TypeScript on our page. We also hope that the article explains how to do it. Please comment below if you have any further questions about this question, and we will respond as soon as possible! We appreciate your time spent reading.
Read more
→ Setting Optional Parameters In Functions Using TypeScript – How To Do It?
Leave a comment