Creating a Typescript interface that includes double arrow functions is a great way to define the

How can a typescript interface be written using the double arrow function es6 syntax?

JavaScript Example:

const myFunction => (param1) => (param2) => {
...code
}

TypeScript Example:

const myFunc = (param1: number) => (param2: number) => {
  return param1 + param2
};

This interface appears to have an error

interface myInterface {
   myFunc: (param1: number) => (param2: number) => number
}

The error message received is: Parsing error: ';' expected Why does this error occur and what is the correct syntax to use?

Answer №1

It appears that the issue lies within your JavaScript code.

const myFunction => (param1) => (param2) => {
...code
}

The syntax used here is not valid in JavaScript. Perhaps you meant to write it like this:

const myFunction = (param1) => (param2) => {
...code
}

The remaining code seems to be compiling correctly on my end:

interface MyInterface {
   myFunc: (param1: number) => (param2: number) => number
}

const Foo: MyInterface = {

  myFunc: (param1: number) => (param2: number) => {
    return param1 + param2
  }

}

class FooClass implements MyInterface {

  myFunc(param1: number) {
    return (param2: number) => {
      return param1 + param2;
    }
  }

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The module "jquery" in jspm, jQuery, TypeScript does not have a default export

Having some trouble setting up a web app with TypeScript and jspm & system.js for module loading. Progress is slow. After installing jspm and adding jQuery: jspm install jquery And the initial setup: <script src="jspm_packages/system.js"></scri ...

identify when the React component has reached the bottom of the element by reading the

Is there a way to access the scroll handler's event without being able to access offsetTop, scrollTop, etc? class App extends React.Component { handleScroll = e => { console.log(e.target.scrollTop); }; componentDidMo ...

Struggling to translate JavaScript code into Typescript

Currently in the process of converting my JavaScript code to Typescript, and encountering an error while working on the routes page stating Binding element 'allowedRoles' implicitly has an 'any' type. ProtectedRoutes.tsx const Protecte ...

Issue with Angular project: View not being updated when using behaviorSubjects

Within my Angular project, I am retrieving data from an API using a service and storing the data within a BehaviorSubject as shown below private categorySubject = new BehaviorSubject<number | null>(null); apiBehavior = new ReplaySubject<ApiRes ...

Customize back button functionality in Ionic 2

Is it possible to modify the behavior of the back button shown in this image? I would like to specify a custom destination or perform an action before navigating back, instead of simply returning to the previous page. https://i.stack.imgur.com/EI2Xi.png ...

Typescript error when using fill or justify prop in React-bootstrap with Typescript

Code import { useCallback, useState, useEffect } from 'react'; import { Tabs, Tab, Spinner, Alert } from 'react-bootstrap'; import { Categories } from '../../models/ICategory'; import IMovie from '../../models/IMovie&apo ...

The TypeScript error code TS2345 indicates that the argument type 'JQueryXHR' cannot be assigned to the parameter type 'Promise<any>'

In a coding tutorial, an example code snippet demonstrates how to execute a JQuery getJSON() call and then transform the result into a Promise, which is later converted into an Observable. /// <reference path="../typings/tsd.d.ts" /> import { Compo ...

Comparing compiling a TypeScript application and running it with Node.js versus running it with ts-node

Which option is more resource-efficient: compiling my TypeScript application and executing it with Node.js, or using ts-node to run it directly without compilation beforehand? I am working on a TypeScript application that utilizes connections to both MySQ ...

Utilizing Angular and RxJS: A guide on transforming JSON data into an array of objects within a function that returns an Observable

Sharing my hero.service.ts file that was utilized in a recent project: // hero.service.ts import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http'; import { Observable, o ...

Utilize the UserService in NestJs to enhance security within the RolesGuard functionality

In my application, I have a module called UserModule that exports the UserService. Here is an example of how it is done: @Module({ imports: [ MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]), MongooseModule.forFeature([{ name: ...

Using the same selector for Angular 2 components

Is there a way to reuse a component selector with the following structure? @Component({ selector: 'expression-builder', template: ` <div class="container"> <expression *ngFor="#expression of expressions" [prototypes]="prototy ...

What methods can I use to construct a 3D bezier spline using pre-defined 2D (xz) segments while maintaining a consistent "descent rate"?

Currently, I am in the process of developing a small 3D game that incorporates a spline for generating randomized tracks. To better explain this concept visually, think of games like Impossible Road and Audiosurf. I started this project about a week ago us ...

Change the format of the array from the initial array to the new array structure

I have a multi dimensional array that I need to convert into the array2 structure. I've tried various methods, but all of them have resulted in bulky code and lots of iteration. Is there an easier way to accomplish this task? I am new to Angular and ...

Tips for iterating over an array that implements either of two interfaces in TypeScript

The objective is to develop a reusable method for filtering out items from an array that implements one of two interfaces Providing a code example would be most helpful: interface IDuration { start: number; end: number; } interface IRelativeDuration ...

Steps for displaying a loader after refreshing data using queryClient.invalidateQueries:1. Utilize the query

I am currently working on a project where I need to redirect to a specific page after updating an item. While the code is functioning correctly, I have encountered an issue with the loader not displaying. export const useUpdateStatusArchiveSurvey = () => ...

Innovative approaches to enhancing Feathers services through the use of relational data in design patterns

I'm in the process of developing a straightforward application that involves a one-to-many relationship between data entities. Specifically, I am working with feathers js and sequelize (utilizing sqlite) to create a system where each site can have mul ...

Why is there empty white space showing in the select option drop down, even though the data is being bound correctly

When creating a dynamic dropdown on Angular 7 at runtime, I am facing an issue where there is whitespace visible in the select options. Despite correctly binding and filling the data, this white space persists. How can I remove this blank area when binding ...

The return value depends on the type of function argument passed

I am currently developing a type-safe JSON:API specification parser for handling API responses that can either contain a single object or an array of objects (). For example, when making a request like GET /article: { data: { type: 'article&ap ...

Issues with the inheritance functionality in styled components are causing errors

The issue arises when I try to customize the styling of my PrimaryButton component, separate from the DefaultButton. Despite writing style properties for it, the changes do not take effect. Here is the folder structure: https://i.stack.imgur.com/0KjyH.pn ...

How come my push function is not functioning properly on Ionic?

When working with ionic 3, I encountered an issue with the alert controller while trying to push an element into my array. Despite following the necessary steps of receiving parameters and pushing them, I keep encountering a significant error when attempti ...