In Vue 3, I'm creating a function that will accept an instance of a component and a property to be passed. I'm also using TypeScript and wondering if I can enter these parameters. For example, the function would look like:
const example = (component, props) => {
//
};
So my question is:
You can use the many features provided by typescript and the
Componenttypes in vue to achieve proper typing, create a generic type that extendsComponentand then infer the component options/props usinginfer, 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 properties and component instance utilities