TypeScript Vue Component with Generics

I'm currently trying to provide a generic type in a Vue component, but I'm unsure of how to do this from the caller.

Take the following component as an example:

export default class Toggle<T> extends Vue {
    @Prop({ default: {} })    private payload!: T;
    @Prop(Function)           private handleCallback!: (payload: T) => Promise<any>;
}

How can I specify the type for T when calling it like this?

<toggle :payload="{propOfT: 'value of T'}" :handleCallback='someCallbackFn'/>

Answer №1

perhaps utilizing

extend<Data, Methods, Computed, Props>(config?: ThisTypedComponentOptionsWithRecordProps<V, Data, Methods, Computed, Props>): ExtendedVue<V, Data, Methods, Computed, Props>;



interface Data {
  dataProp: boolean
}

interface Methods {
  methodOne: (name: Type) => Promise<void>
}

interface Props {
  items: Item[]
}

export default Vue.extend<Data, Methods, {}, Props>({ ...

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

Tips for preventing circular dependencies when using combineSlices in Redux-toolkit

When utilizing combineSlices with createAsyncThunk condition, I find it challenging to avoid circular dependency. My store initiation thunk looks like this: thunk.ts export const initiateFx = createAsyncThunk< InitiatePayload, string, { state: R ...

Master the art of iterating through an Object in TypeScript

I need help with looping through an Object in TypeScript. While the following examples work in JavaScript, I understand why they pose a problem in TypeScript. Unfortunately, I am struggling to find the correct approach to solve this issue. Am I approaching ...

Trigger a change event for a Material Checkbox by referencing its unique identifier

<div *ngFor="let cus of deselectedList | keyvalue" (click)="clickCheckBox('customer_'+cus.key+'_checkbox')"> {{cus.key}} <mat-checkbox id="customer_{{cus.key}}_checkbox" (change ...

What is the reason behind the triggering of actions by ngrx entity selectors?

I'm currently in the process of learning NgRx, but I'm struggling to comprehend why entity selectors would trigger actions. Despite my efforts to find an explanation, I have come up short. It's possible that I may be missing some fundamental ...

Incorporate a personalized JavaScript code segment during the Vue build process

I am currently working with Vue CLI and I need to include a custom javascript section in the default template when I release my project. However, I do not want this section to be included during the debugging phase. For example, I would like to add the fo ...

`Vue testing utilities fail to refresh component following click event`

My current testing setup involves using vue-test-utils/mocha-webpack/expect to test the component code below: <template> <div> <div id="agent-customer-container" class="top-tabs" v-if="agentName || customerName || miniBasket"> ...

Having trouble with my ag-grid context menu not functioning properly

I am aiming to display a context menu when the user right-clicks on a cell. Unfortunately, my code is not responding to the right click event. I have been unable to locate where I may have made a mistake. Here is the snippet of my code that seems to be c ...

Hide the tab in React Native's bottom tab navigation when on the current screen within the navigator

Currently, I am delving into learning react native. My project consists of 4 screens, yet I only require 3 buttons on my tab navigator. The objective is to hide or eliminate the active screen's tab from being accessible. Specifically, when on the home ...

Leveraging Angular and HTML to efficiently transfer the selected dropdown value to a TypeScript method upon the user's button click

I am experiencing difficulty accessing the .value and .id properties in {{selectItem}} in order to send them back to the typescript for an HTTP post at a later time. Although there are no specific errors, I have encountered exceptions and have tried search ...

Creating a dynamic matrix table in Angular 2

I am looking to create a dynamic matrix table using Angular 2 that displays test results in a specific format: Test1 Test2 Test3 A 1,5 1,8 1,6 B 1,8 1,6 1,9 C 1,6 1,6 1,8 This example data demonstrates the structure ...

Retrieve data from a JSON file URL using a GET request and save the response to be accessed globally when the Vue.js application is initialized

Consider this scenario - I have a Vue.js component where I need to display the name of a user based on their ID. The only information I have is the user's ID. However, I also have a JSON file URL that contains all the user objects with their names and ...

How can one execute a function within an HTML attribute value using Angular?

I am currently attempting to use the weatherToFontAwesomeIcon(weatherDescription: string) function directly within the HTML file for this component. My goal is to showcase a specific FontAwesome icon based on the weather response from the API. import { Cur ...

Checking for queryParam changes in Angular before ngOnDestroy is invoked

I am looking to conditionally run some code in the ngOnDestroy function depending on changes in the current route. Specifically, when the route changes from /foo to /login?logout=true, and this change is initiated outside of the Foo component. In the ngO ...

Best practices for effectively managing interface design

My current interface looks like this: export interface Folder { name: string; id: number; date: Date; } However, in the actual scenario, the JSON response provides the date as a string type. How should I handle this data transfer between the back-en ...

What steps can be taken to eliminate the 404 error when refreshing an Angular 8 Single Page Application (SPA) without using

In my single page application project, I am utilizing Angular 8. Upon uploading my published code to the IIS server without using hash(#) in routing, I encounter a 404 error when attempting to refresh the page. Can anyone provide assistance on how to res ...

"Utilizing the `useState` function within a `Pressable

Experiencing some unusual behavior that I can't quite figure out. I have a basic form with a submit button, and as I type into the input boxes, I can see the state updating correctly. However, when I click the button, it seems to come out as reset. Th ...

A function in Typescript is created to handle diverse input types in a generic manner

My goal is to create a function that can handle various input types for abstraction purposes. type ContentA = string type ContentB = number type InputA = { name: 'method_a' content: ContentA } type InputB = { name: 'method_b' con ...

Display various components using a dropdown selection

I am seeking guidance on how to display different components based on the selected option. I am unsure of how to write the code for displaying either component one or two. Can you provide any examples or references that may help? <template> <div ...

Using Typescript with Vue to create a reactive exported variable

Scenerio Setup Currently, I have developed a Vue3 + Firebase project which includes an object called userInfo in the main.ts file. The export declaration is as follows: export var userInfo :any = {} It is important to note that the :any declaration is ...

"Learn how to utilize the v-for loop to iterate through data and tailor its display based on the specified model

I just started learning Vue.js, so please excuse any incorrect terminology... https://jsfiddle.net/msoutherton/eywraw8t/203188/ Above is a beginner app I'm working on - it's incomplete but gives an idea of what I want to achieve. I aim to creat ...