How to declare Return Types for Functions in TypeScript

This tutorial shows how to declare and return type in Functions of a Typescript. You can check another post on Fix for Object is possibly null Functions in any language contain two parts as given below

  • Function definition
  • Function caller

Each function has a Function name, body, and return value.

The return value is of valid datatype value in typescript. and javascript does not declare a return type.

In typescript, Each function returns data.

Does typescript allow to declare return type?

It is not compulsory to declare the return type for a function.

Here is a syntax for function return type in javascript

The function return type is declared with type after the function followed by a colon.

functionName() : returntype { ... }

For example, In the below example, Declare a class with the method welcome() in the HelloWorld class and the return type does not declare with type.

Typescript compiler infers the type from the return value and assumes that the function return’s string.

Even though it is valid and executes fine in typescript.

class HelloWorld {
  welcome() {
    return "Hello World Example "; // type inferred to be a string
  }
}

Here is an example of declaring a function with a Boolean type

class Utils {
  IsNull(str: string): boolean {
    if (str) {
      return true;
    }
    return false;
  }
}

The recommended approach is always to declare a function with valid return types.

class HelloWorld {
  welcome(): string {
    return "Hello World Example "; // type inferred to be a string
  }
}

How to return multiple data types in the typescript function?

Functions in typescript can return multiple types using union type.

Syntax

functionName() : returntype1|returntype2 { ... }

Let’s write an example of the Equality of a string

function isEqual(str1: string, str2: string): boolean | string {
  return str1 == str2 ? true : "Not Equal";
}

In the above, the function checks for strings equal or not and returns either boolean or string.

Similarly, New types created using type keyword syntax

type StringAndBoolean = boolean | string;
function isEqual(str1: string, str2: string): StringAndBoolean {
  return str1 == str2 ? true : "Not Equal";
}

How to declare function return void type?

a function that returns another function. And also Functions can return functions with the void return type.

use ()=>void to return function void return type

parentFunction(): () => void {
    return () => {
        console.log('Parent function print');
    };
}

Anonymous function returns type declaration

Anonymous functions are functions without function names. It uses the arrow function(=>).

Declare type after colon(:) symbol. Below is an anonymous function that returns the string type.

const getMessage = (): string => "hello test";

For example for return type void

const printLog = (): void => console.log("hello test");

Function declare object returned from a function

Object in the typescript of type interface or class.

Let’s declare an object using the interface

interface Employee {
  id: number;
  name: string;
}

The below function returns an object with multiple properties Object return type declares after colon with the type of an object.

function insertEmployee(id: number, name: string): Employee {
  // DB operation
  return {
    id: 1,
    name: "john",
  };
}