Guide to resolving the error "Type 'void' cannot be assigned to type 'Function' in VueJS"

I've created a Vue component that requires a function pointer to execute a delete action.

<template>
  <q-card class="my-card" >
    <q-img :src="media.normal || media.original">
      <div class="absolute-bottom text-center">
        {{ media.title || 'No Title'}}
      </div>
    </q-img>

    <q-card-section>
      <div class="text-h6">{{ media.title}}</div>
    </q-card-section>

    <q-card-section class="q-pt-none">
      <q-btn @click="actionDelete(media.uuid)">Delete</q-btn>
    </q-card-section>
  </q-card>
</template>

<script setup lang="ts">
import { PropType, ref } from 'vue';
import { MediaItem } from 'src/data/mediaItem';

const props = defineProps({
  media: {
    type: Object as PropType<MediaItem>,
    required: true
  },
  actionDelete: {
    type: Function,
    required: true
  }
});
</script>

After referring to documentation and tutorials, I'm passing the function from the parent component to the child component like this:

<MediaItemComponent :media="media" :actionDelete="actionDelete(media.uuid)" />

However, VueJs (or TypeScript?) is showing this error message:

ERROR(vue-tsc)  Type 'void' is not assignable to type 'Function'.

Can someone point out what I might be doing incorrectly?

Answer №1

A type error indicates a mistake in the code. The use of

:actionDelete="actionDelete(media.uuid)"
causes the actionDelete prop to be set as the result of calling actionDelete, which is undefined in this scenario. The prop is actually expected to be a function and it should not be called with the same argument twice.

In Vue, passing a callback through a prop is rarely necessary since Vue events are available to handle this, making debugging easier using devtools.

The correct setup should be in the parent component:

<MediaItemComponent :media="media" @delete="actionDelete" />

And in the child component:

<q-btn @click="$emit('delete', media.uuid)">

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

What is the best way to remove a selected item from an array in Vuejs when it has been clicked

I'm having trouble with my splice() method in this simple To-Do app. I want to delete a specific task by clicking a button, but the solutions I've tried from Stack Overflow aren't working for me. The items do get deleted, but only the last o ...

Utilizing Angular 5 routerLink for linking to absolute paths with hash symbols

I am facing an issue with a URL that needs to be opened in a new tab. Unfortunately, Angular generates this URL without the # symbol. Currently, we have implemented the following: <!-- HTML --> <a title="Edit" [routerLink] = "['/object/objec ...

Route.get() is expecting a callback function, however it received an object of undefined instead

In my nodejs application using typescript, I am working on separating the routing by introducing interfaces and controllers to handle the logic. app.ts const countryRoutes = require('./routes/countryroute') app.use('/countries', count ...

Exploring the possibilities of developing WebComponents within Angular using TypeScript

My Angular application was originally created using the default method with ng new project-name". However, for performance reasons, I had to incorporate single standard WebComponents. The JavaScript code associated with these components is stored in a ...

Proper usage of a method within another method in a Vue component

Currently, I am extracting GPS data from an image using exifjs. My objective is to convert the latitude and longitude variables into a decimal variable as illustrated below: <template lang="html"> <div class="upload-wrap"> <button cla ...

Creating a hyperlink to a subdomain in TypeScript Execution File

I am seeking to add a link directing to a subdomain in the navigation bar of a react theme. Although I feel a bit hesitant about asking for help on something seemingly simple, I have struggled to find any relevant examples online to assist me. Below is the ...

Efficiently implementing state and dispatch for useReducer in TypeScript with React

I'm encountering an error in my current setup. The error message reads: 'Type '({ team: string | null; } | { team: string | null; } | { ...; } | { ...; } | { ...; } | Dispatch<...>)[]' is missing the following properties from t ...

Encountering an Angular 13 ChunkLoadError during application deployment, despite the presence of the respective chunk

We encountered an issue with our application's upgrade from Angular 11 to 13. While running 'ng serve' on the local machine works fine, deploying it to our Azure app service causes the lazy loaded modules to fail loading. The specific error ...

Accessing the OSRM API allows users to determine the distance to the closest emergency station

I am currently working on a typescript project where I need to calculate the distance to the nearest police station and fire station. My approach involves utilizing typescript for this task. Initially, I attempted to use the following URL that I discovere ...

Tips for customizing the appearance of a label when a MUI Radio Button is selected

Hello everyone, I am attempting to customize the label text color of a radio button to turn blue when selected. https://i.stack.imgur.com/btSc2.jpg HERE IS THE CODE FOR MY MUI BUTTON SO FAR import * as React from "react"; import Radio from &quo ...

Issue with Laravel Nova's Tool.vue: Arrow functions are not functioning properly in computed properties

I am facing an issue with a computed property that is not functioning correctly. After referring to the Vue 2.x documentation, here is the code I have: <template> <div> <button :disabled="isDisabled">Import configurator data ...

What is the best way to trim a string property of an object within an array?

I am seeking a solution to access the "description" property of objects within an array and utilize a string cutting method, such as slice, in order to return an array of modified objects. I have attempted using for loops but have been unsuccessful. Here ...

In Vue.js datalist, "label" is not showing up as expected, instead, "[Object object]" is being displayed

In our project, I have implemented a <datalist> drop-down menu to showcase group names. <data-list ref="groupName" name="groupName" label="Groups: " :options="groupList" ></data-list> ..... methods:{ groupList(){ ...

Displaying an array of errors in Vue.js can be done easily by using backend validation with Laravel

I am struggling with displaying the validation error array data in my Vue file due to the complex nature of the data. The issue arises when I try to show data that has an index and is displayed as contacts.0.name: ["...."]. Any suggestions on how ...

Having trouble with the API authentication call, receiving a "Failed to load resource: net::ERR_CONNECTION_REFUSED" error message

I am facing an issue with my Vue and .net application. Whenever I run "NPM start serve," it successfully builds the app. The app is running locally at: http://localhost:8080/ However, when I attempt to log in, I encounter the following error: Console err ...

Learning to extract and interpret stream+json data in Vuejs

Within my vuejs application, I am utilizing axios as the http client and implementing the following code: axios.get('http://localhost:8081/pets') .then(response => { this.pets = response.data; }) Everything works fine when the ...

Using b-icon with the <td> tag in VueJS

I am looking to incorporate HTML content within a table data element using VueJS. Below is an illustration of my situation: <template> <div> <div v-if="someObject.properties" style="margin-top: 20px;" class="table-responsi ...

When trying to use `slug.current` in the link href(`/product/${slug.current}`), it seems to be undefined. However, when I try to log it to the console, it is displaying correctly

import React from 'react'; import Link from 'next/link'; import { urlFor } from '../lib/clients'; const Product = ({ product: { image, name, slug, price } }) => { return ( <div> <Link href={`/product/ ...

"Error: The specified object does not have the capability to support the property or method 'includes'." -[object Error]

Recently, I developed a method that utilizes both indexOf() and includes(). However, I encountered an error message stating "Object doesn't support property or method 'includes'". I have attempted to run the method on both Internet Explorer ...

When attempting to call an API using the Angular HttpClient, an issue is encountered

I am having trouble displaying my JSON data in my application. I have imported the HttpClientModule in app.module.ts, but I keep getting an error that says: "" ERROR Error: Cannot find a differ supporting object '[object Object]' of ty ...