TypeScript: implementing function overloading in an interface by extending another interface

I'm currently developing a Capacitor plugin and I'm in the process of defining possible event listeners for it.

Previously, all the possible event listeners were included in one large interface within the same file:

export interface Plugin {
 
 addListener(
    eventName: 'bannerShow',
    listenerFunc: (info: any) => void,
  ): PluginListenerHandle;

 addListener(
    eventName: 'bannerHide',
    listenerFunc: (info: string) => void,
  ): PluginListenerHandle;

 addListener(
    eventName: 'rewardShow',
    listenerFunc: (info: SomeInterface) => void,
  ): PluginListenerHandle;

 addListener(
    eventName: 'rewardHide',
    listenerFunc: (info: string) => void,
  ): PluginListenerHandle;

}

To make it easier to maintain, I have decided to separate the listeners into different files like this:

export interface Plugin extends Banner, Reward {
  // General plugin calls here
}

// Separate file for Banner interface
export interface Banner {

specificBannerFunction(): void;

addListener(
    eventName: 'bannerShow',
    listenerFunc: (info: any) => void,
  ): PluginListenerHandle;

 addListener(
    eventName: 'bannerHide',
    listenerFunc: (info: string) => void,
  ): PluginListenerHandle;
}

// Separate file for Reward interface
export interface Reward {

 specificRewardFunction(): string;
 addListener(
    eventName: 'rewardShow',
    listenerFunc: (info: SomeInterface) => void,
  ): PluginListenerHandle;

 addListener(
    eventName: 'rewardHide',
    listenerFunc: (info: string) => void,
  ): PluginListenerHandle;
}

The issue arises when Typescript throws the following error message:

Interface 'Plugin' cannot simultaneously extend types 'Banner' and 'Interstitial'. Named property 'addListener'of types 'Banner' and 'Interstitial' are not identical

How can I inform TypeScript that I am overloading that function call?

Answer №1

When it comes to TypeScript, I'm still relatively new myself. I'm not entirely sure if you can achieve this using Interfaces, but by changing your Plugin interface to a type, you could potentially utilize an intersection.

For instance, instead of:

export interface Plugin extends Banner, Reward {
  // General plugin calls here
}

You might consider:

export type Plugin = Banner & Reward & { /* General plugin calls here */ }

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

Updating the navigation bar in Node/Angular 2 and displaying the sidebar once the user has logged in

I am facing a challenge with my first project/application built using Angular 2, particularly related to the login functionality. Here is what I expect from the application: Expectations: When I load the login component for the first time, the navbar ...

Dealing with Errors in Node.js Using Typescript and Express

As I embark on my journey with Node and Typescript, a question has cropped up in my mind. There are two key files in my project: server.ts import express = require('express'); import IConfiguration from "../config/config"; export default clas ...

Guide to importing a class property from one file to another - Using Vue with Typescript

Here is the code from the src/middlewares/auth.ts file: import { Vue } from 'vue-property-decorator' export default class AuthGuard extends Vue { public guest(to: any, from: any, next: any): void { if (this.$store.state.authenticated) { ...

Seeking a solution to the useRef problem. Encountering difficulties with React Hook useRef functionality within a NextJS application

Whenever I refresh the page, the 'Ref' value is displayed as null. This causes the if condition blocks not to work. I attempted to modify the useRef values but could only set it to null. When I console log the myDivRef.current, it returns "Ref: ...

What is the significance of static constants?

In my TypeScript project, I'm working on creating a constants file that will provide an Opaque token object when ServiceConstants.AUTH_SERVICE_TOKEN is called. I've experimented with two approaches: Using ServiceConstants.ts as a namespace exp ...

The use of 'import ... =' is restricted to TypeScript files

Error: Oops! Looks like there's a hiccup in the code... 'import ... =' is exclusive to TypeScript files. Expecting '=' here. Don't forget the ';'. Unexpected keyword or identifier popping up! package.json ...

Developing advanced generic functions in Typescript

I am currently working on a Hash Table implementation in Typescript with two separate functions, one to retrieve all keys and another to retrieve all values. Here is the code snippet I have so far: public values() { let values = new Array<T>() ...

Should an HTML canvas in Angular be classified as a Component or a Service?

I have a basic drawing application that uses an MVC framework in TypeScript, and I am looking to migrate it to Angular. The current setup includes a Model for data handling, a View for rendering shapes on the canvas, and a Controller to manage interactio ...

Double-executing methods in a component

I have encountered an issue while trying to filter existing Worklog objects and summarize the time spent on each one in my PeriodViewTable component. The problem I am facing involves duplicate method calls. To address this, I attempted to reset the value ...

The error message "ng: command not found" popped up despite successfully installing the latest @angular/cli using npm linking

Here is the information about my current setup: Node version: v10.15.3 NPM version: 6.4.1 I attempted to run the following command: Command: npm i -g angular/cli An error occurred while executing: npm ERR! /usr/local/bin/git ls-remote -h -t ssh:// ...

Retrieve the value of a property with a generic type

Within our angular6 project, we encountered an issue while using the "noImplicitAny": true setting. We realized that this caused problems when retrieving values from generic types. Currently, we retrieve the value by using current['orderBy'], bu ...

Learning how to use arrow functions with the `subscribe` function in

Can someone help clarify the use of arrow functions in TypeScript with an example from Angular 2's Observable subscribe method? Here's my question: I have code that is functional: this.readdataservice.getPost().subscribe( posts =&g ...

The name 'Diagnostics' cannot be located

I've downloaded the Typescript repository and am currently reviewing the code. However, I keep encountering this recurring error message: Cannot find name 'Diagnostics' This error pops up on lines that are similar to this: Diagnostics._ ...

Incorporating a swisstopo map from an external source into an Angular

I am looking to integrate a swisstopo map into my angular 8 app. As I am new to angular, I am unsure how to include this example in my component: I have tried adding the script link to my index.html file and it loads successfully. However, I am confused a ...

Dynamically importing TypeScript interfaces for React code splitting

Is it possible to utilize dynamic import('path') for an exported interface? ...

What steps should I take to resolve the 'invalid mime type' issue while transmitting an image as a binary string to Stability AI via Express?

Currently, I am facing an issue while attempting to utilize the image-to-image API provided by stabilityAI. The task at hand involves sending an image as a binary string through my express server to the stability AI API. However, when I make the POST reque ...

Searching for values within an array of objects by iterating through nested arrays to apply a filter

Having trouble returning the testcaseid from an array to this.filteredArray Able to fetch header value and all values of the array when the search word is empty. Seeking assistance with iterating through the testcaseid and header on the search input fiel ...

Guide on utilizing TypeScript interfaces or types in JavaScript functions with vscode and jsdocs

Is there a way to utilize types or interfaces to provide intellisense for entire functions or object literals, rather than just function parameters or inline @type's? For example: type TFunc = ( x: number ) => boolean; /** * @implements {TFunc} ...

Node_modules seem to be missing

After completing the TypeScript 101 QuickStart tutorial using Visual Studio 2015 and Node Tools for Visual Studio, I attempted to import the 'winston' npm module. However, no matter what path I specify, Visual Studio indicates that it cannot loca ...

Ways to encourage children to adopt a specific trait

Let's discuss a scenario where I have a React functional component like this: const Test = (props: { children: React.ReactElement<{ slot: "content" }> }) => { return <></> } When a child is passed without a sl ...