Issue with Vue.js Typescript when setting a debounced function

Upon debouncing a function in my Vue.js application, I encountered the following error message:

Type 'DebouncedFunc<(queryParams: QueryParams, page?: number) => Promise<void>>' is not assignable to type '(queryParams: QueryParams, page?: number) => Promise<void>'

As I tried to implement the debounce feature, I faced this issue.

created() {
    newData = _.debounce(newData 1000)
  }

Is there a solution to this problem without resorting to TS-ignore or making extensive changes?

Your input is greatly appreciated.

Answer №1

The error message you encountered suggests that the debounced function's type does not match the expected type.

To resolve this issue, it is important to ensure that the debounced function has the correct type assigned to it.

One solution is to explicitly specify the type of the debounced function using the DebouncedFunc type from the lodash library. Here's an example of how you can define the type of the debounced function:

import { DebouncedFunc } from 'lodash'

created() {
  const debouncedNewData: DebouncedFunc<(queryParams: QueryParams, page?: number) => Promise<void>> = _.debounce(this.newData, 1000)
}

Alternatively, you can also directly define the type of the function being debounced, like so:

created() {
  const newDataFn: (queryParams: QueryParams, page?: number) => Promise<void> = this.newData
  const debouncedNewData = _.debounce(newDataFn, 1000)
}

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

Issue with action creator documentation not displaying comments

We are exploring the possibility of integrating redux-toolkit into our application, but I am facing an issue with displaying the documentation comments for our action creators. Here is our old code snippet: const ADD_NAME = 'ADD_NAME'; /** * Se ...

Having trouble with the functionality of JQuery drop feature?

As I delve into implementing drag and drop functionality with JQuery, I encounter a peculiar issue. I have set up 3 'draggable' divs and corresponding 3 'droppable' divs. The intended behavior is for each draggable element to be accepte ...

How Keyof can render an object undefined and prevent accurate verification

Encountering TS2532 error: Object is possibly 'undefined' while attempting to access an object's value by dynamically selecting the key. TypeScript seems to be restricting me from checking the field values, and I'm unsure of the underly ...

Tips for keeping the header section anchored to the top of the page

I am having trouble getting the menu bar and logo to stick at the top of my header section in the template. I have been trying different solutions, including using the sticky.js plugin for almost 4 days now but it's not working. I also have parallax e ...

Using Javascript to Assign Value to Variables

I have been working on this code snippet: http://jsfiddle.net/9B84H/26/ function autosuggest() { var input = document.getElementById('location'); var options = {types: [],}; var autocomplete = new google.maps.places.Autocomplete(input, o ...

Exporting a module from a TypeScript definition file allows for seamless sharing

I am in the process of developing a definition file for the Vogels library, which serves as a wrapper for the AWS SDK and includes a property that exports the entire AWS SDK. declare module "vogels" { import AWS = require('aws-sdk'); export ...

Creating a Vue application without integrating Bootstrap into the build process

Currently, I am in the process of learning how to develop a Vue app, but I am encountering some difficulties regarding Bootstrap and JQuery integration. The platform where the app is meant to be deployed already has Bootstrap and JQuery integrated into it ...

Finding a solution to this issue in Angular will consistently result in a false outcome

Can anyone explain why I keep getting the error message "This condition will always return 'false' since the types 'typeof ChartType' and 'ChartType' have no overlap" whenever I try to compare charTypeEnum with ChartType.text? ...

Discover the steps to download web page data using Objective-C while ensuring that JavaScript has finished executing

I attempted something similar: NSString *url = @"http://www.example.com"; NSURL *urlRequest = [NSURL URLWithString:url]; NSError *error = nil; NSString *htmlContent = [NSString stringWithContentsOfURL:urlrequest encoding:NSUTF8StringEncoding error:&e ...

What is the best way to stop div animations when clicking with jQuery?

Upon loading the page, a div animates automatically. There is also a button present. When the button is clicked, I would like to create a new div and animate it the same way as the first one. However, when this happens, the position of the first div also ...

When trying to save Canvas as JPG/PNG, the image comes back empty

Currently, I am facing an issue with the canvg library while trying to convert an SVG file to a JPG/PNG format. It seems like the function is not recognizing the id of the SVG block element and as a result, I am getting a blank image. I wonder what could b ...

transferring an EJS object to a Vue function

When working with an Express app, I encountered an issue where I need to pass an object to a Vue method. Below is the snippet of EJS template: <%-item.price%> <button v-on:click="add_to(<%=item%>)" class="float-right">Add...</button& ...

What is the correct syntax for implementing scrollTop and transform CSS effects in jQuery?

I find myself in a bit of a quandary, as this question may seem rather simple but it's causing me some confusion. I'm trying to navigate the CSS transform syntax within a jQuery script that calculates window height. Here is the code in question: ...

The Motion of Rotating and Moving Items

Rotating and translating objects can be tricky. While you can successfully perform both actions, the challenge arises when rotating objects as their orientation gets lost -- causing the objects to move in the direction they are facing. if( keyboar ...

JSON conversion error: Unable to convert circular structure to JSON - locate problem within JSON structure

Currently, I am utilizing Contentful in conjunction with a MEAN stack. Upon querying the Contentful API, I receive a json object. contentClient.entries(query, function(err, entries){ if (err) throw err; console.log(entries); }); Lately, I have been e ...

There seems to be an issue with the CSS file linking properly within an Express application

Every time I run my app.js file, the index.html file is displayed. However, when I inspect the page, I notice that the CSS changes are not taking effect. Strangely, if I open the HTML file using a live server, the CSS changes are visible. Can someone exp ...

"Encountering an issue with Material UI where the Theme Style typography is not

Trying to update typography in the theme using Material UI but facing issues with object changes not working. The palette, however, is functioning correctly. Attempts were made to modify H3 styles and default font size but without success. On the contrar ...

Why isn't the correct component being shown by react-router?

I have encountered an issue with my code. When I type localhost:8080 in the browser, it displays the App.js component as expected. However, upon navigating to localhost:8080/#/hello, it still shows the App.js component instead of hello.js. Interestingly, ...

WebSocket connection was unsuccessful. Switching to Comet and resending the request

I have been utilizing the Atmosphere framework 2.0.0.RC5 to expand my web application with websocket capabilities and encountered a perplexing error 'Websocket failed. Downgrading to Comet and resending' that I can't seem to resolve. To sta ...

The requested module components/Home could not be located by NativeScript

Upon completion of generating a Vue NativeScript app and launching it with the following command: tns preview An error is displayed: *** Terminating app due to uncaught exception 'NativeScript encountered a fatal error: Error: Could not find module ...