import { inject as vueInject, InjectionKey, ComponentInternalInstance, getCurrentInstance } from 'vue' export function inject(key: InjectionKey | string): T { const value = vueInject(key) if (value === undefined) { throw `Failed to inject value with key ${String(key)}` } return value } type ComponentInstanceWithProvide = ComponentInternalInstance & { provides: Record } | null export function injectFromSelfOrAncestor(key: InjectionKey): T { const vm = getCurrentInstance() as ComponentInstanceWithProvide const value = vm?.provides[key as symbol] if (value !== undefined) { return value as T } return inject(key) }