Reputation: 2365
In Vue 3, I'm creating a function that will accept an instance of a component and props to pass through. I'm also using TypeScript and was wondering if I can type those parameters. For example, the function would be something like:
const example = (component, props) => {
//
};
So my questions would be:
Upvotes: 3
Views: 2091
Reputation: 1
You could use many feature provided by typescript and the Component
type from vue to achieve your proper typing, make a generic type that extends the Component
then infers the component options/props using infer
, use Partial to make them optional :
import type { Component } from "vue";
function example<T extends Component>
(Comp: T, props: T extends Component<infer P> ? Partial<P> : never) {
//....
}
example(Alert, { variant: "success"})
Note: this also infers the attributes and component instance utilities
Upvotes: 2