UNPKG

vue-atoms

Version:

Better type-safe provide() and inject() for Vue

20 lines (19 loc) 1.24 kB
import { type InjectionKey } from 'vue'; interface AtomTrait { __type: 'atom'; } type Atom<T = any> = symbol & AtomTrait & InjectionKey<T>; type AtomType<T> = T extends Atom<infer V> ? V : never; export declare const atom: <T>(initialValue: T) => Atom<T>; /** Extend TypeScript overloads of original inject */ export declare function inject<T>(key: Atom<T>): T; export declare function inject<T>(key: InjectionKey<T> | string): T | undefined; export declare function inject<T>(key: InjectionKey<T>, defaultValue: T, treatDefaultAsFactory?: false): T; export declare function inject<T>(key: string, defaultValue: T, treatDefaultAsFactory?: false): unknown | T; export declare function inject<T>(key: InjectionKey<T>, defaultValue: T | (() => T), treatDefaultAsFactory: true): T; export declare function inject<T>(key: string, defaultValue: T | (() => T), treatDefaultAsFactory: true): unknown | T; export declare function provide<T, K extends Atom | InjectionKey<any> | string = string>(key: K, value: K extends Atom ? AtomType<K> : T): void; /** Type-restricted functions */ export declare const injectAtom: <T>(atm: Atom<T>) => T; export declare const provideAtom: <T>(atm: Atom<T>, value: T) => void; export { injectAtom as useAtom };