Calls to event listeners with stringent typing

I'm encountering some difficulties with strong typing in relation to this._onResize and onMouseDown.

At line 28, the error

ESLint: Unsafe call of an any typed value.(@typescript-eslint/no-unsafe-call)
is thrown when onMouseDown(event, this); is called.

Lines 37-38 throw the error

S2769: No overload matches this call.
when
this._handle.removeEventListener('mousedown', this._onResize);
and
this._handle.removeEventListener('touchstart', this._onResize);
are used.

The detailed explanation states:

TS2769: No overload matches this call.   Overload 1 of 2, '(type: keyof ElementEventMap, listener: (this: Element, ev: Event) => any, options?: boolean | EventListenerOptions | undefined): void', resulted in the following error.     Argument of type '"mousedown"' is not assignable to parameter of type 'keyof ElementEventMap'.   Overload 2 of 2, '(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions | undefined): void', led to the following error.     Argument of type '((event: MouseEvent | TouchEvent) => void) | null | undefined' is not assignable to parameter of type 'EventListenerOrEventListenerObject'.       Type 'undefined' is not assignable to type 'EventListenerOrEventListenerObject'.

The relevant code snippet is shown below:

import { Renderer2 } from '@angular/core';

export class ResizeHandle {
  protected _handle: Element | undefined | null;
  private _onResize: ((event: MouseEvent | TouchEvent) => void) | undefined | null;

  constructor(
    protected parent: Element,
    protected renderer: Renderer2,
    public type: string,
    public css: string,
    private onMouseDown
  ) {
    // implementation details omitted for brevity...
  }

  dispose(): void {
    if (this._handle) {
      this._handle.removeEventListener('mousedown', this._onResize);
      this._handle.removeEventListener('touchstart', this._onResize);
      if (this.parent) {
        this.parent.removeChild(this._handle);
      }
    }
    this._handle = null;
    this._onResize = null;
  }

  get element(): Element | undefined | null {
    return this._handle;
  }
}

What specific types should be assigned to these variables?

Answer №1

Your solution works perfectly fine, but the issues you're encountering are due to TypeScript typing errors.

The first issue regarding the unsafe call linting error is because onMouseDown in the constructor signature lacks a specified type. This leads ESLint to flag it as 'any' and triggers an error when called, violating the no-unsafe-call rule.

To resolve this problem, adjust the constructor signature as follows:

constructor(
    protected parent: Element,
    protected renderer: Renderer2,
    public type: string,
    public css: string,
    private onMouseDown: (event: MouseEvent | TouchEvent, rh: ResizeHandle) => void
  ) {

The second problem arises from having strictFunctionTypes set to true and attempting to pass a strongly-typed event listener to removeEventListener that requires only an EventListener type. The workaround involves casting _onResize to EventListener in dispose() like so:

this._handle.removeEventListener('mousedown', this._onResize as EventListener);
this._handle.removeEventListener('touchstart', this._onResize as EventListener);

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

Struggling to Decode Octet-stream Data in Angular 6 HttpClient: Encountering Parsing Failure with Error Prompt: "Failed to parse HTTP response for..."

Is there a way to make a non-JSON request to the server using Angular 6 HttpClient (@angular/common/http) in order to receive an Octet-stream? Below is the code I have tried: getFile(file: any) { let headers = new HttpHeaders({ 'Content-T ...

My project encountered an error when trying to serve with Angular-cli (property 'slice' of null cannot be read)

Everything was running smoothly with my project until now, but after making some modifications to a node_modules file, I encountered an issue. Whenever I attempt to use the "ng" command, I receive the following error message: /usr/lib/node_modules/angula ...

Storing a Vue/JS element reference in a constant using Typescript

In my template, I have one form element and one button element: <button type="submit" id="ms_sign_in_submit" ref="submitButton" class="btn btn-lg btn-primary w-100 mb-5"> </button> Wi ...

Upon attempting to send an HTTP POST request from Angular to Node using Express.js, the operation became stuck with a status 204 indicating no content. The

I'm currently using client-side Ionic with server-side Node.js + Express.js for testing on my local computer. While I can successfully do a POST request using Postman, I am facing difficulties making it work through Ionic. Investigation I have spen ...

What steps should I take to resolve the error message "ESLint encountered an issue determining the plugin '@typescript-eslint' uniquely"?

Struggling to enable eslint linting in an ASP.NET Core MVC project that incorporates React.js and typescript? I'm facing a tough challenge trying to resolve the error mentioned above. In my setup, I'm using Visual Studio 2022 Community Edition 1 ...

How to create an Angular 2 template HTML loop to check for true or false in an

For example: Here is a sample Object: { "Person": { "Name": {}, "Hobbies": { "0": { "Description:Soccer": {}, "IsActive:false": {} }, "1": { "Description:Hockey": {}, "IsActive:false": {} ...

Issue: Typescript/React module does not have any exported components

I'm currently facing an issue with exporting prop types from one view component to another container component and using them as state type definitions: // ./Component.tsx export type Props { someProp: string; } export const Component = (props: ...

Setting a value in Ionic 3 HTML template

Attempting to assign a value in an Ionic 3 template from the ts file while also adding css properties but encountered an issue. PROBLEM Error: Uncaught (in promise): Error: No value accessor for form control with name: 'image' Error: No va ...

Confounding Typescript Type Bindings

I am facing an issue with my Typescript component that uses react-jss and the classes object for styling. The error message I'm getting is: Binding element 'classes' implicitly has an 'any' type., and I'm struggling to find a ...

Obtain the default/initial argument type of typescript for extension purposes

Currently, I am in the process of developing code that automatically generates type hints based on function calls related to GraphQL Nexus tasks. In one of its functions, it requires an anonymous type constructed from the attributes generated automaticall ...

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 ...

Utilizing aframe in conjunction with Angular 2

Currently I am attempting to integrate Aframe into my angular 2 project. Although I have imported the library in my index.html file, I am still encountering difficulties using the aframe directive. For instance: <a-scene> <a-box color="red"& ...

React/Typescript: The object might be null

I am currently converting a React component to TypeScript. The component is making an API call, and although I believe my interfaces are correctly set up, I seem to be passing the types incorrectly. How can I resolve the two errors in the Parent componen ...

What are the steps to defining a static constant within a TypeScript class?

What is the best way to define a TypeScript static constant within a class so that it can be accessed without initializing the class instance? Below is an example of my class structure: export class CallTree{ public static readonly active = 1; .. ...

Defining the type of a React component class constructor in TypeScript that includes a specific static method

After successfully implementing regular classes, including subclasses, with TypeScript, I encountered an issue when working with React components. The explanation provided by TypeScript was limited. import React from 'react' type Props = { tes ...

Maintaining type information while iterating over an object with Typescript

I am faced with the challenge of wrapping functions within an object in order to use their return values, all without altering their signature or losing type information. // An object containing various functions const functions = { foo, bar, baz } // Exa ...

Encountered a hiccup during the deployment of an Angular application on Heroku

I've been working on deploying an Angular app called "test-app" to Heroku via GitHub and everything seems to be going smoothly, except for setting up the express routing function. I've tried different paths, but Heroku keeps throwing this error: ...

Unable to showcase dropdown options in Angular 5

I am currently diving into Angular 5 and following a tutorial. I encountered an issue while attempting to display drop down values in a form. The component.html code provided below is not showing the drop down values. Here's what I have tried so far. ...

Is there a way to update a BehaviorSubject value without using the next method?

I am looking for a solution to update the value of my BehaviorSubject without causing any subscriptions to be triggered. I attempted the following: this.mySubject = new BehaviorSubject(newVal); Unfortunately, this approach also removes all existing subs ...

Angular build issue: "Failed to locate module: Error: Unable to resolve index.ngfactory"

We are encountering a curious error specifically when running the commands ng build --prod or ng build --aot. Strangely, this issue is only happening in our development branch and not in the production branch. Despite thorough comparison, we have found no ...