{

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 other 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 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 function followed by a colon.

functionName() : returntype { ... }

For example, In the below example, Declare a class with 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 for 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 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';
}

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 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 of 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"
    };
};
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.