Hi Friends đź‘‹,

Welcome to Infinitbility ❤️!

Today, we will learn to make functions with their return data type, as we know typescript provides static typing and it’s not limited to defining variables only.

Here, we will create a function and specify their return data type and use it from another place.

let’s understand return type function syntax.

functionName() : ReturnType { ... }

In syntax we have to create a function : and their return data type then starts your function code within curly braces.

let’s create a sample example…

class User {
    name: string;
    constructor (message: string) {
        this.name = message;
    }
  
    getUserName() : string {
        return "Hello, " + this.name
    }
}

let result = new User().getUserName();
typeof result;

Output

TypeScript, return type function example
TypeScript, return type function example

And if you are finding an example of constant function syntax check the following code.

const GetName: string = () => {
  return "infinit"
}

All the best đź‘Ť.