Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generics in TypeScript Vue Component

I am trying to supply generic type into vue component, but don't know how to supply that from the caller.

For example, for the component below

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

how would I supply type for Twhen it is called as below?

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

like image 872
Bishal Paudel Avatar asked Jul 31 '19 01:07

Bishal Paudel


1 Answers

maybe you can use

extend<Data, Methods, Computed, Props>(options?: 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>({ ...
like image 187
user2809176 Avatar answered Sep 18 '22 20:09

user2809176