Navigate back to the homepage

How to define return type of function in typescript?

Infinitbility
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 this.name = message;
5 }
6
7 getUserName() : string {
8 return "Hello, " + this.name
9 }
10}
11
12let result = new User().getUserName();
13typeof 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.

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

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

Tutorials

Categories

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
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]