Navigate back to the homepage

How to define return type of function in typescript?

March 23rd, 2022 · 1 min read
keep affilitate

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.

1functionName() : 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…

1class User {
2 name: string;
3 constructor (message: string) {
4 = message;
5 }
7 getUserName() : string {
8 return "Hello, " +
9 }
12let result = new User().getUserName();
13typeof result;


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.

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

All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

melio affilitate

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide



More articles from Infinitbility

How to get specific data from json in reactjs?

React, Learn to get specific data from json.

March 20th, 2022 · 1 min read

How to make a div scrollable in react js?

React, Learn to make scrollable div with vertically and horizontally using css.

March 19th, 2022 · 1 min read
© 2020–2022 Infinitbility
Link to $ to $ to $ to $ to $ to $mailto:[email protected]