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:
P粉9174060092024-01-06 09:36:48
You can use the many features provided by typescript and the Component
types in vue to achieve proper typing, create a generic type that extends Component
and then infer 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 properties and component instance utilities