{

How to declare Return Types for Functions in TypeScript


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

The return value of any type of value and javascript does not declare return type.

In typescript, Each function returns data.

Does typescript allow to declare return type?

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

Here is a syntax for function return type

Function return type is declared with type after function followed by colon.

functionName() : returntype { ... }

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

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

Eventhough it is valid and executes fine in typescript.

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

Here is an example for declaring a function with 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 infered to be string
    }
} 

Function return multiple data types

Functions in typescript can return multiple types using union type.

Syntax

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

Let’s write an example for Equality of 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.

Similary, New types creates using type keyword syntax

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

Declare return void function 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 arrow function(=>).

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

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

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;
}

Below function returns an object of multiple properties Object return type declares after colon with type of an object.

function insertEmployee(id: number,name: string) : Employee {
// db operation
    return {
        id: 1,
        name: "john"
    };
};
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.