What is the best way to retrieve the argument type of an SFC component's event?

Imagine I have a situation where there is an SFC component containing a save emit:

// ChildComponent.vue
const emit = defineEmits<{
  save: [data: { new: Row[]; removed: Row[] }];
}>();

If I needed to access the new and removed properties in a parent component, this would be the approach:

// ParentComponent.vue
<ChildComponent 
  @save="($event) => {
    performAPIRemove($event.removed); // TypeScript can correctly infer the type of `$event`
  }"
/>

However, let's say I want to define the event callback as a named function. How could I determine the type of the child component's emit?

// ParentComponent.vue
// A possible solution might involve...
function saveCallback(event: typeof ChildComponent.emits.save) {
  performAPIRemove(event.removed)
}

Answer №1

My recommendation is to start by creating a new interface in your .ts file, for example:

export interface EmitChildren {
  add: Item[];
  update: Item[];
}

Next, within your child component, you can define the emit function using this type:

const emit = defineEmits<{
  action: [payload: EmitChildren];
}>();

Finally, when defining your callback function, ensure it accepts the ChildEmit parameters, like so:

function handleAction(event: EmitChildren) {
  executeChanges(event.update);
}

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 compiling Tailwind styles in Angular 16 development environment

Encountering an issue with Tailwind version 3.4.1 while using dev mode in an Angular application with version 16.2.12. It appears that Tailwind is not detecting changes and fails to recompile the CSS after saving. The styles refresh only once when the .ang ...

Every time I submit the form, I aim to create a unique random string in the format of 'ZXCVBN'

Is there a way to automatically create a random 6-character string consisting of uppercase letters each time a form is submitted, and then assign this string to the 'code' parameter in the event array within the add-event.comonent.ts file? The g ...

How to efficiently update a child component in React using UseState and establish a connection back to the parent component

I am currently working on developing a prototype for a master/detail scenario in React and Material-UI. The task involves creating a basic list of objects with the ability to edit and save an item using a dialog. While I have successfully updated the visit ...

What is the best method to locate an element<T> within an Array[T] when <T> is an enum?

I've recently started learning TypeScript and exploring its functionalities. I could use some assistance in deepening my understanding. Within our angular2 application, we have defined an enum for privileges as follows: export enum UserPrivileges{ ...

CSRF validation did not pass

I am currently working on a project in Django where I am trying to send an image file using XMLHttpRequest(). Below is the script I am using: $('#edit_user_image').change(function(){ var client = new XMLHttpRequest(); var file = document ...

When attempting to access the ref of a Text Input in a React Native Component, the refs are found

In my application, I have multiple components each containing a textInput field. When a user submits the textInput in one component, I want it to automatically focus on the next component's textInput field. However, when I try to implement this functi ...

What is the best way to define the typings path for tsify?

My TypeScript sources are located in the directory: src/game/ts The configuration file tsconfig.json can be found at: src/game/ts/tsconfig.json Additionally, the typings are stored in: src/game/ts/typings When running tsc with the command: tsc --p s ...

Troubleshooting Bootstrap Select and dynamic rows problem in a Vue.js project

I am facing an issue with my dynamic table/rows in vue.js where I am using bootstrap-select to enhance the dropdown select. My form includes an add/remove line feature for dynamic functionality. However, I am unable to load the options of a select using bo ...

Retrieve the data entered in the submit button field

My question concerns a form with two buttons: <form method="post" action=""> <button type="submit" name="age" value="20">Age 20</button> <button type="submit" name="age" value="30">Age 30</button> </form> When ...

The use of `slot` attributes in Ionic has been deprecated and flagged by the eslint-plugin-vue

I encountered an error message while using VS Code: [vue/no-deprecated-slot-attribute] `slot` attributes are now considered deprecated. eslint-plugin-vue https://i.sstatic.net/DUMLN.png After installing two plugins in .eslintrc.js, I have the following c ...

How can I update jQuery CSS method on resize event?

Having trouble vertically centering a div inside the body? I've come up with a jQuery function that calculates the ideal margin-top value for achieving perfect vertical alignment. Here's how it works: Obtain container height, Get child height, ...

How come TypeScript tuples support the array.push method?

In the TypeScript code snippet below, I have specified the role to be of Tuple type, meaning only 2 values of a specified type should be allowed in the role array. Despite this, I am still able to push a new item into the array. Why is the TS compiler not ...

Utilize the Twitter Bootstrap modal feature to play autoplaying Youtube videos that automatically pause when

Similar Question: How do I halt a video using Javascript on Youtube? I am curious about how I can utilize the modal feature in Twitter Bootstrap to automatically play a Youtube video, and when a user clicks the "close" button, it will cease the video ...

ways to instantly showcase the input's value within a DIV element

For example, I have a div with the ID of "someDiv" and an input text field with the ID of "someInput". How can I make it so that the value entered into the input field displays in the DIV in real time? For instance, if I type the letter "a" in the input fi ...

Transforming characters in a string using Javascript

Currently, I am replacing commas in a textbox. However, how do I go about replacing both a "$" and a comma if they appear together in the same line? function doValidate() { var valid = true; document.likeItemSearchForm.sup.value = document.likeItemSearc ...

Unable to pass a component property to a styled Material-UI Button

I have customized a MUI Button: const SecondaryButton = styled(Button)<ButtonProps>(({ theme }) => ({ ... })); export default SecondaryButton; When I try to use it like this: <label htmlFor="contained-button-file"> <input ...

Share information by including the provider within the @component declaration in Angular

I am looking to explore a new method of passing data using providers directly within the component itself. For instance, I am curious if there is a way to pass a variable from one component to another without relying on routing or other traditional methods ...

Incorporate meta tags containing images and titles into your Angular 6 project

I am currently exploring ways to include an image and title when sharing the URL of a specific page in my project. I have attempted the following method: createMetaTag() { const meta = document.createElement('meta'); meta.setAttribute(&a ...

Send JSON information using a POST request through the Fetch API and retrieve it on the Express backend server

Currently, I am in the process of learning Express and front-end JavaScript. My aim is to pass JSON data via a POST request using the Fetch API and then handle it on the backend with Express. The backend code I have written looks like this: const express ...

Challenges faced with the Nativescript Software Development Kit

I am currently working on a Nativescript app with Angular and using a JSON server. However, I am facing some errors when I try to run 'tns run android' or 'tns doctor' commands. × The ANDROID_HOME environment variable is either not se ...