import { defineComponent, createApp, h, provide, ref, InjectionKey, Ref } from 'vue'; type InstanceType = V extends { new (...arg: any[]): infer X } ? X : never; type VM = InstanceType & { unmount(): void }; export function mount(Comp: V) { const el = document.createElement('div'); const app = createApp(Comp); // @ts-ignore const unmount = () => app.unmount(el); const comp = app.mount(el) as any as VM; comp.unmount = unmount; return comp; } export function useSetup(setup: () => V) { const Comp = defineComponent({ setup, render() { return h('div', []); }, }); return mount(Comp); } export const Key: InjectionKey> = Symbol('num'); export function useInjectedSetup(setup: () => V) { const Comp = defineComponent({ setup, render() { return h('div', []); }, }); const Provider = defineComponent({ components: Comp, setup() { provide(Key, ref(1)); }, render() { return h('div', []); }, }); return mount(Provider); }