Inferred and Explicit Return Types

Learn via video courses
Topics Covered

Overview

Return types in a function are nothing but the value which we want to return from the function. We can return any type of value from the function. Functions also return value along with control, back to the caller. Such functions are called returning functions. The return types can be a string, number, boolean, etc. If we do not return the correct value from the function then we will get an error. There are two types of return types in a function.

Introduction

Functions are the building blocks of a programming language.TypeScript has a specific syntax for typing function parameters and return values.

There is only one way of defining the return type of a function that is explicit. If we do not write the return type explicitly then the compiler will infer the type itself.

The type is also used when the function does not return any value. Now let's learn about each of the types in detail:

Inferred Return Types in Typescript

Inferred return types mean that we do not give any return type to the compiler as a result the compiler infers the returns types, but the compiler may be wrong sometimes and our code might get an error due to this. If no return type is defined, TypeScript will attempt to infer it through the types of the variables or expressions returned. Let's look at examples with inferred type in typescript.

Here since we have not written the return the compiler will infer itself and the return type will be a number in this case. Run this code in your editor for a better and clear explanation. The return type of a function is also inferred by the returning value. Let's see another example:

Here we will get an error even after defining the return type explicitly because the result can be stored in a number-type variable but not a string-type variable. Hence type inference is helpful in type-checking when there are no explicit type annotations available. Run the code in your editor for a better explanation. Now let's read about the disadvantages of inferred return types in typescript:

Disadvantage of Inferred Return Types

When we do not give any return type to the function that is when the compiler has to infer the type and the return type on its own then the compiler may be wrong and may show the wrong return type this is the biggest problem in inferred return types. Let's see an example where the inferred return type is causing a problem:

Suppose we have a function like this and if any changes are made and the inferred type causes an error in the code or infers the wrong type, this is why we should declare the return type explicitly.

Now suppose we write out the return type like this then any other person who will read or makes changes in the code will have a clear idea that the return type will always be a number. This is why we should write out the function type every time. Run this code in your editor for a better and clear explanation.

Explicit Return Types

We can use the explicit return type to declare the return type of the function, hence declaring the return type explicitly does the compiler not have to infer it. The type of value returned by the function can be explicitly defined.

Syntax

The syntax is similar to how we declare functions just there is one difference here we have defined the return type of the function. Now let's see a few examples of how we can use explicit return types in our program:

By writing the return type which is number anyone who writes or makes changes in our code will be damn sure that the return type of this function will be number only. Run this code in your editor for a better and clear explanation. Now let's see one more example:

As we know that typescript assumes all the parameters as essential which are passed in the function, but we can mark the parameters which we will not require as optional explicitly. In the above function, we have marked num3 as optional. Run the code in your editor for a better explanation.

The main benefit of using explicit return types is not that whoever might call the function, but rather that whoever can edit the function in the future.

Conclusion

  • If we declare the types explicitly then the person who reads our code will know the return type of the function.
  • Inferred types help check when there are no explicit types declared.
  • Compiler may always not infer the correct type and the compiler may cause an error thus declaring the return types explicitly is a good practice.
  • We can declare the void return type when a function doesn't return any value.
  • Declaring the types explicitly is a good habit as the developer or the person who is writing the code will know its return type.