Functions and Interfaces Working Together in TypeScript

Functions and Interfaces Working Together in TypeScript

How to add types to functions and use interfaces in TypeScript

How to add types to functions and use interfaces in TypeScript

Welcome to the land of TypeScript! A superset of the JavaScript language where every value has a type associated with it.

If you are new to TypeScript, check out the article below to get an introduction to type annotation and type inference.

In this article, we will take a look at how to handle types in functions. We will first look at how to annotate function types, then take a look at how to use interfaces with functions.

Adding types to functions

When adding types to functions, there are two places to be aware of. Types for the arguments, and the type for the return value.

To annotate types for arguments, simply add a colon and the type next to the parameter.

To add a type to the output, add a colon after the argument list with the type of the return value. The code below shows a basic function with the types annotated. Now, TypeScript knows that the add function will accept two arguments of type number, and return a value with type number.

const add = (a: number, b: number): number => {
  return a + b;
};

Image for post

You can do the same using named functions and anonymous functions as well.

function add(a: number, b: number): number {
  return a + b;
};

const add = function(a: number, b: number): number {
  return a + b;
};

software-development typescript programming web-development javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

TypeScript Tutorial For JavaScript Developers - TypeScript Basics

TypeScript Tutorial For JavaScript Developers - TypeScript Basics. I will show you guys 4 example of JavaScript code, and how to convert it to TypeScript. This is a typescript beginners tutorial.

Software Developer vs Web Developer | Difference Web Developer & Software Developer

Software Developer vs Web Developer | Difference Web Developer & Software Developer | Software development and web development are normally used interchangeably. Although both include development, a software developer has different responsibilities from that of a web developer.

ERP Software Services, POS Software Services , Application Development

Vinew Technologies focused majorly over ERP Software services and POS Software services, Web & Application development services & Woo commerce and Wordpress easy-to-use, multipurpose social media plugin for WordPress.

5 Core Criteria for Selecting Software Development Company - TopDevelopers.co

Check out these five criteria for the selection of your software vendor, and you will never regret having the wrong quality product made for you.